Why doesn't Haskell accept arguments after a function composition?

后端 未结 2 1905
轻奢々
轻奢々 2021-01-22 16:23

Considering Haskell has currying functions, we can do this:

foo a b = a + b -- equivalent to `foo a = \\b -> a + b`

foo 1 -- ok, returns `\\b -> 1 + b`
fo         


        
2条回答
  •  深忆病人
    2021-01-22 17:01

    bar . foo 1 2 is bar . (foo 1 2) not (bar . foo 1) 2

    There's nothing mysterious going on here related to lambdas. Say we expanded the application of foo to 1:

    bar . foo 1 2
    bar . (\b -> 1 + b) 2
    

    Now, we apply the lambda to the 2

    bar . 3
    

    And there is your problem.

    Conversely, if we place the parentheses correctly, we evaluate it like this:

    (bar . foo 1) 2
    (bar . (\b -> 1 + b)) 2
    (\x -> bar ((\b -> 1 + b) x)) 2
    bar 3
    

提交回复
热议问题