Cochemuacos
Cochemuacos

Reputation: 102

Implementing an interpreter in Haskell exercise CIS194

I'm lerning haskell from the material that can be found here. I'm begining homework 3 in which the objective is to write an interpreter for a simple language but I'm stuck in the first exercise:

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

I don't get what I'm supposed to be doing here. In the hint I'm not sure what "black box" means. In the example I get that st' is a State, but I'm not sure I get what in st' "A" == 5 is doing.

If someone could clarify both the hint and the example for me I think I would be able to solve this exercise.

Upvotes: 0

Views: 199

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 152682

"Black box" means you can ignore how it's implemented and just use it. let foo = bar in baz binds a new variable foo to the expression bar, then results in baz (which may mention foo); presumably here the point is that the given Haskell expression should evaluate to True -- that is, this is a test case for your implementation.

Upvotes: 3

Related Questions