pls fast help me with this simple question

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 -- View this message in context: http://www.nabble.com/pls-fast-help-me-with-this-simple-question-t1345292.ht... Sent from the Haskell - Haskell-Cafe forum at Nabble.com.

On 3/26/06, iliali16
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
participants (2)
-
iliali16
-
Sebastian Sylvan