Haskell: Function application with $

后端 未结 4 1701
滥情空心
滥情空心 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:28

    $ has lower precedence then : (and also anything else) so your function is parsing as

    (n : collatz') $ (n `div` 2)
    

    This leads to your type error. The second argument of : expects a list but you are passing the collatz function instead.

    If you still want to avoid the parenthesis around the 3n+1 part you can do something like the following

    (n:) . collatz' $ n `div` 2
    n : (collatz' $ n `div` 2)
    

    although these are not necessarily cleaner then the original. In case you are wondering, the (n:) in the first example is a syntactic sugar for \x -> n : x

提交回复
热议问题