3
我使用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
当我测试程序:
aState = extend empty "A" 5
bState = extend aState "B" 4
cState = extend bState "C" 3
我想这cState
应相当于一个功能:
\x -> if x == "C"
then 3
else if x == "B"
then 4
else if x == "A"
then 5
else 0
但是相反,我得到cState "B" == 0
和cState "A" == 0
。
我看不出extend
有什么问题,有人可以向我解释吗?