Leave a comment

Comments 12

Maybe anonymous August 6 2010, 06:57:10 UTC
Isn't that should be added?
return Nothing = Nothing

PS: your tutorial is very intuitive and nice. Done a good job!

Reply

Re: Maybe mvanier August 6 2010, 08:44:36 UTC
Thanks! I have two more articles nearly ready to post, so stay tuned. Anyway, return for the Maybe monad is just Just, because its argument is arbitrary (it's not restricted to be a Maybe type). Written out fully, it's:

return :: a -> Maybe a
return x = Just x
-- or just:
-- return = Just

Notice that a can be any type whatsoever, not just a Maybe type. The value returned from return (I know that sounds weird) will be a Maybe type, but the argument can be of any type.

Reply

Re: Maybe mvanier August 6 2010, 09:15:49 UTC
It just occurred to me that I didn't fully answer your question. So, to be exact: return Nothing = Just Nothing.

Reply

Re: Maybe ext_248605 September 5 2010, 15:15:59 UTC
I think I may have a related question. If the definition of "return" for the Maybe monad is

return x = Just x

then it doesn't seem to match what's built in to Haskell, viz:

*Main> return Nothing
Nothing

and

*Main> return (Just 3)
Just 3

Is there something I'm missing? Your tutorials are awesome, by the way, thanks.

Reply


(Possible) eq. 6 proof & dice roll errors anonymous December 7 2010, 00:18:23 UTC
Hey there,

I’m all very new to this, but I think I may have found some little errors:

1. In the Eq. 6 proof, the lines

= map f x1 : map f (xs ++ y) -- definition of map
= map f x1 : (map f xs ++ map f y) -- inductive hypothesis
= (map f x1 : map f xs) ++ map f y -- definition of ++

the first map on each line should be omitted, according to the definition of the map function.

2. In the dice roll function, the if and else branches don’t have the same types-(n1,n2) vs. [ ]. If I understand this all correctly, the tuple should be made into a list: [(n1,n2)].

Of course, if any of this is wrong, feel free to correct me!

Thanks a lot for these guides. Especially the monadic functions vs. monadic values discussion and the in-depth treatment of example monads are invaluable!

Reply

Re: (Possible) eq. 6 proof & dice roll errors mvanier December 17 2010, 01:16:28 UTC
Thanks for your bug fixes! I'll have a corrected version up shortly. It's embarrassing to see these mistakes, but it's great to have readers who catch them. Thanks also for your positive comments -- I really appreciate them.

Reply

Re: (Possible) eq. 6 proof & dice roll errors mvanier December 17 2010, 01:22:21 UTC
BTW the dice roll function is correct, as you can verify by typing into ghci or copying to a file and running from ghci. The "then" branch of the if is "return (n1, n2)" which is just [(n1, n2)] by the definition of return for the list monad. So both branches of the if have the same type.

Reply


Trivial typo anonymous February 28 2011, 05:43:05 UTC
In your dice roll example, you say "find all pairs of numbers between 1 and 6 that sum to the number 7". It should be "sum to the number 6". Just thought I'd point that out.

Reply

Re: Trivial typo mvanier March 3 2011, 23:53:43 UTC
Thanks for pointing this out! I meant 7, so I'll fix it and repost it shortly.

Reply


Leave a comment

Up