Haskell Precedence: Lambda and operator

前端 未结 2 1488
后悔当初
后悔当初 2021-02-05 08:53

I found precedence and associativity is a big obstacle for me to understand what the grammar is trying to express at first glance to haskell code.

For example,



        
相关标签:
2条回答
  • 2021-02-05 09:23

    A good rule of thumb seems to be that you can never make a custom operator that has precedence over built in syntactic constructs. For instance consider this example:

    if b then f *** x else f *** y
    

    Regardless of the associativity of ***, no one would expect it to binds as:

    (if b then f *** x else f) *** y
    

    There aren't a lot of syntactic constructs in Haskell (do and case are a little special because of layout syntax) but let can be used as another example:

    (let x = y in y *** x) /= ((let x = y in y) *** x) 
    
    0 讨论(0)
  • 2021-02-05 09:27

    The rule for lambdas is pretty simple: the body of the lambda extends as far to the right as possible without hitting an unbalanced parenthesis.

    f (\x -> foo (bar baz) *** quux >>= quuxbar)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                           body
    
    0 讨论(0)
提交回复
热议问题