我正在用Haskell模拟一个评估器.它应该很简单,但我无法调试.
在这里,我定义State为查找函数(String -> Int),初始状态(empty,异常变量求值0),extend并将新键(及其值)添加到base环境中:
type State = String -> Int
extend :: State -> String -> Int -> State
extend base key val = \x -> if key == x
then val
else base key
empty :: State
empty = \x -> 0
Run Code Online (Sandbox Code Playgroud)
当我测试程序时:
aState = extend empty "A" 5
bState = extend aState "B" 4
cState = extend bState "C" 3
Run Code Online (Sandbox Code Playgroud)
我想这cState应该等同于一个函数:
\x -> if …Run Code Online (Sandbox Code Playgroud)