Haskell <- 在 do 符号中
Haskell <- in do notation
众所周知,do
块中的 <-
只是 >>=
的语法糖,但 <-
在 Haskell 源代码或它只是作为语言语法一部分的句法构造,因此解析器只是用适当形式的 >>=
替换每个 <-
吗?
Do 表达式是语言的一部分。
来自 Haskell 2010 年语言报告:
3.14 Do Expression
lexp → do { stmts } (do expression)
stmts → stmt1 … stmtn exp [;] (n ≥ 0)
stmt → exp ;
| pat <- exp ;
| let decls ;
| ; (empty statement)
A do expression provides a more conventional syntax for
monadic programming. It allows an expression such as
putStr "x: " >>
getLine >>= \l ->
return (words l)
to be written in a more traditional way as:
do putStr "x: "
l <- getLine
return (words l)
As indicated by the translation of do, variables bound by let have fully polymorphic types while those defined by <- are lambda bound and are thus monomorphic.
请参阅 Haskell 语言报告的 Do Expression。
众所周知,do
块中的 <-
只是 >>=
的语法糖,但 <-
在 Haskell 源代码或它只是作为语言语法一部分的句法构造,因此解析器只是用适当形式的 >>=
替换每个 <-
吗?
Do 表达式是语言的一部分。
来自 Haskell 2010 年语言报告:
3.14 Do Expression
lexp → do { stmts } (do expression) stmts → stmt1 … stmtn exp [;] (n ≥ 0) stmt → exp ; | pat <- exp ; | let decls ; | ; (empty statement)
A do expression provides a more conventional syntax for monadic programming. It allows an expression such as
putStr "x: " >> getLine >>= \l -> return (words l)
to be written in a more traditional way as:
do putStr "x: " l <- getLine return (words l)
As indicated by the translation of do, variables bound by let have fully polymorphic types while those defined by <- are lambda bound and are thus monomorphic.
请参阅 Haskell 语言报告的 Do Expression。