做标记法
do
-notation 是 monad 的语法糖。以下是规则:
do x <- mx do x <- mx
y <- my is equivalent to do y <- my
... ...
do let a = b let a = b in
... is equivalent to do ...
do m m >> (
e is equivalent to e)
do x <- m m >>= (\x ->
e is equivalent to e)
do m is equivalent to m
例如,这些定义是等效的:
example::IO Integer
example =
putStrLn "What's your name?" >> (
getLine >>= (\name ->
putStrLn ("Hello, " ++ name ++ ".") >> (
putStrLn "What should we return?" >> (
getLine >>= (\line ->
let n = (read line::Integer) in
return (n + n))))))
example::IO Integer
example = do
putStrLn "What's your name?"
name <- getLine
putStrLn ("Hello, " ++ name ++ ".")
putStrLn "What should we return?"
line <- getLine
let n = (read line::Integer)
return (n + n)