[Haskell-beginners] help using Data.Map lookup - how to get values
after the "Just" constructor
martin.tomko at geo.uzh.ch
Tue Sep 21 09:50:46 EDT 2010
Thank you Edgar, Daniel,
these helped a lot!
one more question regarding the suggestion of daniel, using ! if I KNOW
that the key will occur (and as I work in a closed world, it should be
Out of curiosity - how does one use ! ??
let's say I have a function returning a map: myFunct a b, and I was using:
M.lookup c (myFunct a b)
On this, I could happily use fromJust (M.lookup c (myFunct a b))
Now, if I do
(myFunct a b) ! c i get ! not in scope.
! seems to come from Data.Map and I import it as
import qualified Data.Map as M
I don't think I can do M.! :))). How to go about it?
On 9/21/2010 3:34 PM, Daniel Fischer wrote:
> 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
> 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
> 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.
More information about the Beginners