>>=
和 >>
运算符都是 infixl 1
.为什么是左结合?
特别是,我观察到等价性:
(do a; b; c ) == (a >> (b >> c)) -- Do desugaring
(a >> b >> c) == ((a >> b) >> c) -- Fixity definition
所以
do
与固定性定义的自然工作方式不同,这令人惊讶。
最佳答案
>>=
肯定是左结合的。
Prelude> ["bla","bli di","blub"] >>= words >>= reverse
"albilbidbulb"
Prelude> ["bla","bli di","blub"] >>= (words >>= reverse)
<interactive>:3:30: error:
• Couldn't match expected type ‘[[b0]]’
with actual type ‘String -> [String]’
• Probable cause: ‘words’ is applied to too few arguments
In the first argument of ‘(>>=)’, namely ‘words’
In the second argument of ‘(>>=)’, namely ‘(words >>= reverse)’
In the expression:
["bla", "bli di", "blub"] >>= (words >>= reverse)
和
>>
几乎跟随>>=
;如果它有另一个固定性,它不仅会像 Lennart 所说的那样让人感到奇怪,而且还会阻止您在链中使用这两个运算符:Prelude> ["bla","bli di","blub"] >>= words >> "Ha"
"HaHaHaHa"
Prelude> infixr 1 ⬿≫; (⬿≫) = (>>)
Prelude> ["bla","bli di","blub"] >>= words ⬿≫ "Ha"
<interactive>:6:1: error:
Precedence parsing error
cannot mix ‘>>=’ [infixl 1] and ‘⬿≫’ [infixr 1] in the same infix expression
关于haskell - 为什么 Haskell monadic 是左结合的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49470200/