gpt4 book ai didi

haskell - 为什么 Haskell monadic 是左结合的?

转载 作者:行者123 更新时间:2023-12-03 23:29:18 25 4
gpt4 key购买 nike

>>=>>运算符都是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com