[Haskell-cafe] do notation strangeness
Felipe Lessa
felipe.lessa at gmail.com
Sat Dec 8 11:59:16 EST 2007
Hello!
I see from http://www.haskell.org/haskellwiki/Monads_as_computation#Do_notation
that
do { v <- x ; <stmts> }
= x >>= \v -> do { <stmts> }
However, look at this GHCi session:
Prelude> let return' = return :: a -> Maybe a
Prelude> do {1 <- return 1; return' "ok"}
Just "ok"
Prelude> return 1 >>= \1 -> return' "ok"
Just "ok"
Prelude> do {1 <- return 3; return' "ok"}
Nothing
Prelude> return 3 >>= \1 -> return' "ok"
*** Exception: <interactive>:1:13-30: Non-exhaustive patterns in lambda
Hmmm... let's try with IO:
Prelude> let return' = return :: a -> IO a
Prelude> do {1 <- return 1; return' "ok"}
"ok"
Prelude> return 1 >>= \1 -> return' "ok"
"ok"
Prelude> do {1 <- return 3; return' "ok"}
*** Exception: user error (Pattern match failure in do expression at
<interactive>:1:4)
Prelude> return 3 >>= \1 -> return' "ok"
*** Exception: <interactive>:1:13-30: Non-exhaustive patterns in lambda
Oh! What about lists?
Prelude> let return' = return :: a -> [a]
Prelude> do {1 <- return 1; return' "ok"}
["ok"]
Prelude> return 1 >>= \1 -> return' "ok"
["ok"]
Prelude> do {1 <- return 3; return' "ok"}
[]
Prelude> return 3 >>= \1 -> return' "ok"
*** Exception: <interactive>:1:13-30: Non-exhaustive patterns in lambda
Something seems wrong to me here. What am I missing?
Thanks!
--
Felipe.
More information about the Haskell-Cafe
mailing list