[Haskell-beginners] Trouble Understanding How to Start
Bob Ippolito
bob at redivi.com
Sat Oct 18 16:26:18 UTC 2014
What is `get` supposed to do when there is no variable equal to `x`? The
type should be `Name -> Env -> Maybe Int`, there should be some additional
argument for the default value, or there should be some hard-coded default
value.
If you were using a hard-coded default value of `-1` it might look like
this:
get :: Name -> Env -> Int
get x env = case env x of
Just n -> n
Nothing -> -1
On Sat, Oct 18, 2014 at 8:08 AM, Keeley Abbott <kallarhynn at gmail.com> wrote:
> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20141018/6fba5878/attachment.html>
More information about the Beginners
mailing list