在 Haskell 练习 CIS194 中实现解释器
Implementing an interpreter in Haskell exercise CIS194
我正在学习 haskell 从 material 可以找到 here. I'm begining homework 3 其中 objective 是为一种简单的语言编写解释器,但我'我卡在了第一个练习中:
Before we can start evaluating Expressions and Statements
we need some way to store and look up the state of a variable. We
define a State
to be a function of type String -> Int
. This makes it
very easy to look up the value of a variable; to look up the value of
"A"
in state
, we simply call state "A"
. Whenever we assign a variable,
we want to update the program State
. Implement the following
function:
extend :: State -> String -> Int -> State
Hint: You can use the input State as a black box for variables other
than the one you are assigning.
Example:
let st’ = extend st "A" 5
in st’ "A" == 5
我不明白我应该在这里做什么。在提示中我不确定 "black box" 是什么意思。在这个例子中,我得到 st'
是一个状态,但我不确定我得到 in st' "A" == 5
正在做什么。
如果有人能为我阐明提示和示例,我想我就能解决这个练习。
"Black box" 意味着您可以忽略它的实现方式并直接使用它。 let foo = bar in baz
将新变量 foo
绑定到表达式 bar
,然后结果为 baz
(可能会提到 foo
);据推测,这里的重点是给定的 Haskell 表达式的计算结果应为 True
—— 也就是说,这是您实现的测试用例。
我正在学习 haskell 从 material 可以找到 here. I'm begining homework 3 其中 objective 是为一种简单的语言编写解释器,但我'我卡在了第一个练习中:
Before we can start evaluating Expressions and Statements we need some way to store and look up the state of a variable. We define a
State
to be a function of typeString -> Int
. This makes it very easy to look up the value of a variable; to look up the value of"A"
instate
, we simply callstate "A"
. Whenever we assign a variable, we want to update the programState
. Implement the following function:
extend :: State -> String -> Int -> State
Hint: You can use the input State as a black box for variables other than the one you are assigning.
Example:
let st’ = extend st "A" 5
in st’ "A" == 5
我不明白我应该在这里做什么。在提示中我不确定 "black box" 是什么意思。在这个例子中,我得到 st'
是一个状态,但我不确定我得到 in st' "A" == 5
正在做什么。
如果有人能为我阐明提示和示例,我想我就能解决这个练习。
"Black box" 意味着您可以忽略它的实现方式并直接使用它。 let foo = bar in baz
将新变量 foo
绑定到表达式 bar
,然后结果为 baz
(可能会提到 foo
);据推测,这里的重点是给定的 Haskell 表达式的计算结果应为 True
—— 也就是说,这是您实现的测试用例。