
It might help you to remember that you can sort of replace the types in a
function with their equivalents, and it may lead you to realizing how to do
them.
For example:
get :: Name -> Env -> Int
is equivalent to
get :: String -> (String -> Maybe Int) -> Int
So think about how you can take the string you have, put it into that
function that takes a string and gives a maybe int, then how to get the int
out of the maybe int. You'll get it.
On Sat, Oct 18, 2014 at 11:08 AM, Keeley Abbott
Hi All,
So, I am taking a Functional Programming class this fall, and I have struggling to understand how to build types and functions correctly. For some reason it just isn’t clicking. I have the following module I am working on, and I need some guidance.
— Variable names type Name = String
— An environment for looking up the value of a variable type Env = Name -> Maybe Int
— An empty environment empty :: Env empty env = Nothing
— Set a variable to a value in the environment if it doesn’t already exist set :: Name -> Int -> Env -> Env set x i e = \y -> if x == y then (Just i) else e y
— Lookup the value of a variable in the environment get :: Name -> Env -> Int get x env = ?? (I previously had “type Env = Name -> Int”, so this was working with env x, but we had to change empty to be something other than just a runtime error, which is why I made Env = Name -> Maybe Int)
—Remove a variable from the environment unset :: Name -> Env -> Env unset x e = \y -> if x == y then empty x else env y
At this point I don’t know how to fix get, so I get an int to display (it keeps telling me it can’t match the type ‘Maybe Int’ with the expected type ‘Int’). And I don’t know where to go from there to create getOr, setAll, and mapEnv functions, because I am just not understanding what I am doing. For the most part I have been doing simpler things that I could just mess with the functions until they work, but even at that I’m not getting HOW they work… Any assistance or instruction on how I can GET what I am trying to do would be greatly appreciated.
Thanks, Kallarhynn _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners