Haskell: Function application with $

后端 未结 4 1677
滥情空心
滥情空心 2021-01-11 15:00

In the following snippet, you can see my two collatz functions I wrote in Haskell. For the recursive application I used parentheses in the first example (collatz) to get the

4条回答
  •  礼貌的吻别
    2021-01-11 15:07

    : binds more strongly than $. Consider

    Prelude> let f x = [x]
    Prelude> 1 : f 2
    [1,2]
    Prelude> 1 : f $ 2
    
    :1:5:
        Couldn't match expected type `[a0]' with actual type `t0 -> [t0]'
        In the second argument of `(:)', namely `f'
        In the expression: 1 : f
        In the expression: 1 : f $ 2
    

    Note the "expression" 1 : f found by the parser; it sees (1 : f) $ 2 rather than 1 : (f $ 2).

提交回复
热议问题