<div dir="ltr"><div><div><div>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.<br><br>For example:<br><br></div>get :: Name -> Env -> Int<br></div>is equivalent to<br>get :: String -> (String -> Maybe Int) -> Int<br><br></div>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.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Oct 18, 2014 at 11:08 AM, Keeley Abbott <span dir="ltr"><<a href="mailto:kallarhynn@gmail.com" target="_blank">kallarhynn@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi All,<br>
<br>
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.<br>
<br>
— Variable names<br>
type Name = String<br>
<br>
— An environment for looking up the value of a variable<br>
type Env = Name -> Maybe Int<br>
<br>
— An empty environment<br>
empty :: Env<br>
empty env = Nothing<br>
<br>
— Set a variable to a value in the environment if it doesn’t already exist<br>
set :: Name -> Int -> Env -> Env<br>
set x i e = \y -> if x == y then (Just i) else e y<br>
<br>
— Lookup the value of a variable in the environment<br>
get :: Name -> Env -> Int<br>
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)<br>
<br>
—Remove a variable from the environment<br>
unset :: Name -> Env -> Env<br>
unset x e = \y -> if x == y then empty x else env y<br>
<br>
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.<br>
<br>
Thanks,<br>
Kallarhynn<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>