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

标签 haskell monads

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

相关文章:

haskell - 收集两个一元操作的结果

windows - Windows : how to read Unicode input from console? 上的 GHCi

javascript - 将包含单个数据数组的任务链接(或映射)到任务数组

haskell - 在不使用列表的情况下内嵌长字符串

class - haskell make(a类)其他类的实例'

javascript - 好的,*这是* monadic 吗?

haskell - Euler 43 - 是否有一个单子(monad)可以帮助编写此列表理解?

haskell - 这个 State monad 代码是如何工作的?

haskell - 'State' 的数据构造函数在哪里?

haskell - 在 Yesod 中始终为 `$newline never`