Trouble Understanding How to Start

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Trouble Understanding How to Start

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

Reply | Threaded
Open this post in threaded view
|

Trouble Understanding How to Start

Bob Ippolito
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>

Reply | Threaded
Open this post in threaded view
|

Trouble Understanding How to Start

David McBride
In reply to this post by Keeley Abbott
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 <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/cb435026/attachment.html>