[Haskell-beginners] help using Data.Map lookup - how to get values after the "Just" constructor

edgar klerks edgar.klerks at gmail.com
Tue Sep 21 09:37:00 EDT 2010


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 <martin.tomko at geo.uzh.ch>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 <martin.tomko at geo.uzh.ch>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
>> 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/20100921/db33243c/attachment.html


More information about the Beginners mailing list