For Programmers: Free Programming Magazines  


Home > Archive > Functional > May 2005 > Monads









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Monads
Mike Austin

2005-05-13, 4:08 pm

I've been through the cruft of google, and yes there is a lot of
information on monads. But the one that best helped me understand was
the comparison with unix pipes:

http://okmij.org/ftp/Computation/monadic-shell.html

and a little help from:

http://www.haskell.org/tutorial/monads.html

The only thing I am unsure of is how the monadic version below returns
multiple values (in sequence, not in tuples).

[(x,y) | x <- [1,2,3], y <- [1,2,3], x /= y]

do x <- [1,2,3]
y <- [1,2,3]
True <- return (x /= y)
return (x,y)

[1,2,3] >>= (\x -> [1,2,3] >>= (\y -> return (x/=y) >>=
(\r -> case r of True -> return (x,y)
_ -> fail "")))

This is lot legal Haskell code, but if I am correct in my assumptions,
this is a visual representation of the syntactic sugar for monads:

do (x <- [1,2,3]
(y <- [1,2,3]
(True <- return (x /= y)
return (x,y))))


Regards,
Mike
Ralph Becket

2005-05-13, 4:08 pm

I'm not a Haskell expert, but I think the desugaring of the list
comprehension

[(x, y) | x <- [1, 2, 3], y <- [1, 2, 3], x /= y]

is something not far from

[1, 2, 3] >>= \x -> [1, 2, 3] >>= \y -> if x /= y then [(x, y)] else []

and this all works fine provided that >>= (the bind operator) has a
sensible definition such as

[] >>= _ = []
(x:xs) >>= f = (f x) ++ (xs >>= f)

(this definition would be provided in an appropriate instance
declaration).

-- Ralph

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com