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

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

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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Also note that Maybe is a monad. So you don't have to use pattern matching.
Try for example in ghci: fromJust $ Just 1 >>= (\x -> return $ x + 1)
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
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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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
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: 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
mailto:edgar.klerks@gmail.com> 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
mailto:martin.tomko@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@haskell.org mailto:Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Tuesday 21 September 2010 15:37:35, Martin Tomko wrote:
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,
Understanding will improve if you start to use them (don't go in too deep too fast, just start tinkering a little with Monads in Maybe and [], take a look at Control.Monad.State, build up experience and understanding).
and they seem not to be necessary for the simple algebra I am trying to develop. Do I really need to use them here?
Not really. Monads make many things more convenient, but you can do almost all without them. In the case at hand, using Maybe's Monad instance could add some convenience and elegance, but it'd not make a huge difference.
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.)
Apart from avoiding parentheses, ($) has a few other uses, it can be passed to some higher order functions and it's nice to use for sections, map ($ 4) [sin, cos, (^2)] zipWith ($) [sin, cos, (^2)] [1,2,3] In both cases, ($) could be replaced with id, since ($) :: (a -> b) -> a -> b f $ x = f x , ($) *is* the identity function, restricted to function types and with a different fixity, but somehow map ($ 4) looks less intriguing than map (`id` 4), doesn't it?
this seems to be equivalent to fromJust (Just 1),
It's fromJust (Just 2). foo :: Int -> Maybe Int foo x = Just (x+1) The function (\x -> return $ x+1) is just foo, only more general (works for any Num type and any Monad). Just val >>= func = func val, so Just 1 >>= foo = foo 1 = Just (1+1) = Just 2 Nothing >>= _ = Nothing
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?
No, the x is (bound to) the argument of Just on the left of the (>>=), in this case 1. Keyboard input lives in the IO Monad, there's no such stuff available in the Maybe Monad. In getLine >>= \line -> putStrLn (reverse line) the name line is bound to the keyboard input. Just "A line" >>= \line -> putStrLn (reverse line) doesn't type-check, because the Monad to the left of (>>=) is different from the Monad on the right (left: Maybe, right: IO).
Cheers Martin

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
mailto:martin.tomko@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@haskell.org mailto:Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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-Mayb...
Greets,
Edgar
On Tue, Sep 21, 2010 at 3:28 PM, Martin Tomko
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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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

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 the case). 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 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

On Tuesday 21 September 2010 15:50:46, Martin Tomko wrote:
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 the case).
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.! :))).
Too ugly?
How to go about it?
You'd use myFunct a b M.! c if you have import qualified Data.Map as M at the top of your module. However, qualified operators are often ugly, so I suggest import qualified Data.Map as M import Data.Map (Map, (!)) -- to get the type name and the (!) operator for unqualified use myFunct a b ! c or, with parentheses, (myFunct a b) ! c

whoa, that's a cool suggestion, thanks! I finally understand the meaning of importing only some functions explicitly. Very cool. thanks Martin On 9/21/2010 4:18 PM, Daniel Fischer wrote:
On Tuesday 21 September 2010 15:50:46, Martin Tomko wrote:
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 the case).
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.! :))).
Too ugly?
How to go about it?
You'd use
myFunct a b M.! c
if you have
import qualified Data.Map as M
at the top of your module. However, qualified operators are often ugly, so I suggest
import qualified Data.Map as M import Data.Map (Map, (!)) -- to get the type name and the (!) operator for unqualified use
myFunct a b ! c
or, with parentheses,
(myFunct a b) ! c
participants (3)
-
Daniel Fischer
-
edgar klerks
-
Martin Tomko