[Haskell-cafe] pls fast help me with this simple question

Sebastian Sylvan sebastian.sylvan at gmail.com
Sun Mar 26 13:31:16 EST 2006


On 3/26/06, iliali16 <iliali16 at gmail.com> wrote:
>
> type Data = Integer
> type Variable = String
> type Store = [(Variable, Data)]
>
> Store = [("",0)]
> emptystore :: Store
> emptystore = [("",0)]
>
> getVal :: Store -> Variable -> Data
>
> thats my code i just have to continue and get the value of a variable from
> the store but i don't know how the task is :
>
> Define a function getVal, which takes a Store and a variable name,
> and returns its value. You may assume that the variable is defined
> in the Store.
> getVal :: Store -> Variable -> Data
>
> (c) Define a function setVar, which takes a Store, a variable name and a
> value, and returns an updated Store with the variable value updated.
> setVal :: Store -> (Variable, Data) -> Store


So you have a store which is a list of (name,value) pairs. You want to
write a function which takes a name, a store, and returns the value.
So:
> getVal [("x",4),("y",5)] "x"
should return 4

You could either use functions in the standard library (check out
lookup in Data.List and fromJust in Data.Maybe), or just do the
recursion yourself:
getVal [] _ = error "Variable wasn't in the Store, not supposed to happen!"
getVal ((id,val):xs) var = ...

Replace the ... with your own logic. If var is equal to id then you
should return val, else you need to check the rest of the store (xs).


/S

--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862


More information about the Haskell-Cafe mailing list