Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  help using Data.Map lookup - how to get values       after the
      "Just" constructor (Martin Tomko)
   2. Re:  help using Data.Map lookup - how to get      values after the
      "Just" constructor (Daniel Fischer)
   3. Re:  help using Data.Map lookup - how to get      values after the
      "Just" constructor (edgar klerks)
   4. Re:  help using Data.Map lookup - how to get values       after the
      "Just" constructor (Martin Tomko)


----------------------------------------------------------------------

Message: 1
Date: Tue, 21 Sep 2010 15:28:49 +0200
From: Martin Tomko <[email protected]>
Subject: Re: [Haskell-beginners] help using Data.Map lookup - how to
        get values      after the "Just" constructor
To: edgar klerks <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Thank you Edgar,
that helped a lot. I successfully implemented the fromJust option, I am 
not sure how to define the fromMaybe one, which seems superior. I would 
need two parameters (a and Maybe a, is that correct) but not sure how to 
get them.

Any link to understand this better is appreciated.

Apologies for being such a hopeless newbie.

Thanks
Martin

On 9/21/2010 3:15 PM, edgar klerks wrote:
> Hello Martin,
>
> You have two options. You can use fromJust :: Maybe a -> a, which is a 
> partial function, so it can fail if the supplied value is Nothing and 
> gives a hard to track down exception.
>
> Or you can use fromMaybe :: a -> Maybe a -> a,  which returns a 
> default a in case Maybe a = Nothing and a if Maybe a = Just a.
>
> There is also a third: maybe :: b -> (a -> b) -> Maybe a -> b, which 
> can be useful in the last step of some chain of functions. Note that 
> fromMaybe is just (flip maybe id).
>
> Greets,
>
> Edgar
>
> On Tue, Sep 21, 2010 at 3:05 PM, Martin Tomko <[email protected] 
> <mailto:[email protected]>> wrote:
>
>     Dear All,
>     as a newbie, I am only just discovering some intricacies of haskell.
>     I have a Data.Map map, and am trying the lookup function to get
>     the value for a given key (it is a list in my case, btw). I am
>     struggling to get access to the value, as it is constructed using
>     Just. I know that the question is therefore more general then the
>     application on Map, so I would be glad to get a wider picture. I
>     Checked in Real World Haskell, btu did nto find and answer. In
>     Haskell the craft of... I found the following (p263):
>
>     mapValue :: (a->b)-> Maybe a -> Maybe b
>     mapValue g (Just a) = Just (g a)
>     mapValue g Nothing = Nothing
>
>     Which is fine, but it makes the Just constructor travel through
>     the whole code, which is annoying. Is there a way out? Or would
>     that be a dirty hack?
>
>     I do not quite understand the following discussion of maybe
>     (p263-4), but it seems like the code suggested is able to return a
>     value at the end...
>
>     Thanks
>     Martin
>
>     _______________________________________________
>     Beginners mailing list
>     [email protected] <mailto:[email protected]>
>     http://www.haskell.org/mailman/listinfo/beginners
>
>


-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100921/bef73e28/attachment-0001.html

------------------------------

Message: 2
Date: Tue, 21 Sep 2010 15:34:10 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] help using Data.Map lookup - how to
        get     values after the "Just" constructor
To: [email protected], [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

On Tuesday 21 September 2010 15:05:19, Martin Tomko wrote:
> Dear All,
> as a newbie, I am only just discovering some intricacies of haskell.
> I have a Data.Map map, and am trying the lookup function to get the
> value for a given key (it is a list in my case, btw). I am struggling to
> get access to the value, as it is constructed using Just. I know that
> the question is therefore more general then the application on Map, so I
> would be glad to get a wider picture. I Checked in Real World Haskell,
> btu did nto find and answer. In Haskell the craft of... I found the
> following (p263):
>
> mapValue :: (a->b)-> Maybe a -> Maybe b
> mapValue g (Just a) = Just (g a)
> mapValue g Nothing = Nothing

Note: this is a special case of the more general `fmap'. If you use fmap 
instead of mapValue, a) you don't need to write the function yourself (it's 
available from the Prelude) and b) it works unchanged if you find that 
instead of Maybe, e.g. a list is more appropriate.

>
> Which is fine, but it makes the Just constructor travel through the
> whole code, which is annoying. Is there a way out? Or would that be a
> dirty hack?

No, not a dirty hack. But you have to distinguish between the cases where 
your lookup was successful and those where it wasn't.
Depending on the surrounding code, for example

.... case Data.Map.lookup key map of
        Nothing -> stuff for absent key
        Just val -> stuff with val

is a good way. If you *know* that the key you want to look up is in the 
map, you can also use (!) to get the value.

>
> I do not quite understand the following discussion of maybe (p263-4),
> but it seems like the code suggested is able to return a value at the
> end...

maybe :: b -> (a -> b) -> Maybe a -> b
maybe default transform valueOrNot

if valueOrNot is a value (Just val), the result is (transform val), if 
valueOrNot is Nothing, the result is the default.

There's a less general variant (to be imported from Data.Maybe)

fromMaybe :: a -> Maybe a -> a
fromMaybe default valueOrNot

returns val in case valueOrNot is (Just val) and the default in case of 
Nothing.

If you use that, you might as well directly use Data.Map.findWithDefault.

These are good if there's a sensible default for absent keys, otherwise the 
won't help you either.

>
> Thanks
> Martin



------------------------------

Message: 3
Date: Tue, 21 Sep 2010 15:37:00 +0200
From: edgar klerks <[email protected]>
Subject: Re: [Haskell-beginners] help using Data.Map lookup - how to
        get     values after the "Just" constructor
To: [email protected]
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi Martin,

You don't have to implement them. You can import them by typing at the top
level of your module:

import Data.Maybe

I think the definition is something like:

fromMaybe :: b -> (a -> b) -> Maybe a -> b
fromMaybe b f (Just a) = f a
fromMaybe b _ (Nothing) = b

You can look it up here:
http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Data-Maybe.html

Greets,

Edgar


On Tue, Sep 21, 2010 at 3:28 PM, Martin Tomko <[email protected]>wrote:

>  Thank you Edgar,
> that helped a lot. I successfully implemented the fromJust option, I am not
> sure how to define the fromMaybe one, which seems superior. I would need two
> parameters (a and Maybe a, is that correct) but not sure how to get them.
>
> Any link to understand this better is appreciated.
>
> Apologies for being such a hopeless newbie.
>
> Thanks
> Martin
>
>
> On 9/21/2010 3:15 PM, edgar klerks wrote:
>
> Hello Martin,
>
> You have two options. You can use fromJust :: Maybe a -> a, which is a
> partial function, so it can fail if the supplied value is Nothing and gives
> a hard to track down exception.
>
> Or you can use fromMaybe :: a -> Maybe a -> a,  which returns a default a
> in case Maybe a = Nothing and a if Maybe a = Just a.
>
> There is also a third: maybe :: b -> (a -> b) -> Maybe a -> b, which can be
> useful in the last step of some chain of functions. Note that fromMaybe is
> just (flip maybe id).
>
> Greets,
>
> Edgar
>
> On Tue, Sep 21, 2010 at 3:05 PM, Martin Tomko <[email protected]>wrote:
>
>> Dear All,
>> as a newbie, I am only just discovering some intricacies of haskell.
>> I have a Data.Map map, and am trying the lookup function to get the value
>> for a given key (it is a list in my case, btw). I am struggling to get
>> access to the value, as it is constructed using Just. I know that the
>> question is therefore more general then the application on Map, so I would
>> be glad to get a wider picture. I Checked in Real World Haskell, btu did nto
>> find and answer. In Haskell the craft of... I found the following (p263):
>>
>> mapValue :: (a->b)-> Maybe a -> Maybe b
>> mapValue g (Just a) = Just (g a)
>> mapValue g Nothing = Nothing
>>
>> Which is fine, but it makes the Just constructor travel through the whole
>> code, which is annoying. Is there a way out? Or would that be a dirty hack?
>>
>> I do not quite understand the following discussion of maybe (p263-4), but
>> it seems like the code suggested is able to return a value at the end...
>>
>> Thanks
>> Martin
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100921/db33243c/attachment-0001.html

------------------------------

Message: 4
Date: Tue, 21 Sep 2010 15:37:35 +0200
From: Martin Tomko <[email protected]>
Subject: Re: [Haskell-beginners] help using Data.Map lookup - how to
        get values      after the "Just" constructor
To: edgar klerks <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi edgar,
the whole of my code and the input data are one contained world, I am 
not using mondas - as I don't understand them properly, and they seem 
not to be necessary for the simple algebra I am trying to develop. Do I 
really need to use them here?

Now, to your example:
 >>>Try for example in ghci: fromJust $ Just 1 >>= (\x -> return $ x + 1)
(I also had to search for definition of the $ operator, totally avoided 
in the two books I have, and seems to be just syntactic sugar instead of 
parentheses. Argh.)

this seems to be equivalent to fromJust (Just 1), where I would assuem a 
result of 1. But the example seems to be dependent on whatever x is 
entered by keyboard. Am I right?

Cheers
Martin

On 9/21/2010 3:19 PM, edgar klerks wrote:
> Also note that Maybe is a monad. So you don't have to use pattern 
> matching.
>
> You have to import the Data.Maybe to get the functions in the scope.
>
> On Tue, Sep 21, 2010 at 3:15 PM, edgar klerks <[email protected] 
> <mailto:[email protected]>> wrote:
>
>     Hello Martin,
>
>     You have two options. You can use fromJust :: Maybe a -> a, which
>     is a partial function, so it can fail if the supplied value is
>     Nothing and gives a hard to track down exception.
>
>     Or you can use fromMaybe :: a -> Maybe a -> a,  which returns a
>     default a in case Maybe a = Nothing and a if Maybe a = Just a.
>
>     There is also a third: maybe :: b -> (a -> b) -> Maybe a -> b,
>     which can be useful in the last step of some chain of functions.
>     Note that fromMaybe is just (flip maybe id).
>
>     Greets,
>
>     Edgar
>
>     On Tue, Sep 21, 2010 at 3:05 PM, Martin Tomko
>     <[email protected] <mailto:[email protected]>> wrote:
>
>         Dear All,
>         as a newbie, I am only just discovering some intricacies of
>         haskell.
>         I have a Data.Map map, and am trying the lookup function to
>         get the value for a given key (it is a list in my case, btw).
>         I am struggling to get access to the value, as it is
>         constructed using Just. I know that the question is therefore
>         more general then the application on Map, so I would be glad
>         to get a wider picture. I Checked in Real World Haskell, btu
>         did nto find and answer. In Haskell the craft of... I found
>         the following (p263):
>
>         mapValue :: (a->b)-> Maybe a -> Maybe b
>         mapValue g (Just a) = Just (g a)
>         mapValue g Nothing = Nothing
>
>         Which is fine, but it makes the Just constructor travel
>         through the whole code, which is annoying. Is there a way out?
>         Or would that be a dirty hack?
>
>         I do not quite understand the following discussion of maybe
>         (p263-4), but it seems like the code suggested is able to
>         return a value at the end...
>
>         Thanks
>         Martin
>
>         _______________________________________________
>         Beginners mailing list
>         [email protected] <mailto:[email protected]>
>         http://www.haskell.org/mailman/listinfo/beginners
>
>
>


-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100921/e7615e35/attachment.html

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 27, Issue 48
*****************************************

Reply via email to