[Haskell-beginners] Trouble Understanding How to Start
Keeley Abbott
kallarhynn at gmail.com
Sat Oct 18 15:08:16 UTC 2014
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
More information about the Beginners
mailing list