在列表中使用 cons with tail of tail 会抛出错误

Using cons with tail of tail in a list throws an error

我是一个 Haskell 初学者,我基本上是想了解为什么会失败:

Prelude> test
[2,3,4,1]
Prelude> tail $ tail test
[4,1]
Prelude> 2 : [4,1]
[2,4,1]
Prelude> 2 : tail $ tail test

<interactive>:27:1: error:
    • Couldn't match expected type ‘[Integer] -> t’
                  with actual type ‘[Integer]’
    • The first argument of ($) takes one argument,
      but its type ‘[Integer]’ has none
      In the expression: 2 : tail $ tail test
      In an equation for ‘it’: it = 2 : tail $ tail test
    • Relevant bindings include it :: t (bound at <interactive>:27:1)

<interactive>:27:5: error:
    • Couldn't match expected type ‘[Integer]’
                  with actual type ‘[a0] -> [a0]’
    • Probable cause: ‘tail’ is applied to too few arguments
      In the second argument of ‘(:)’, namely ‘tail’
      In the expression: 2 : tail
      In the expression: 2 : tail $ tail test
Prelude>

我看到错误消息“可能的原因:‘tail’应用于太少的参数”,但我不太了解导致它的原因,因为同一个表达式刚刚起作用。

您需要注意运算符 grouping/precedence:2 : tail $ tail test 与“cons 2 onto the value tail $ tail test”的区别与 5 * 3 + 2 不同“将 3 + 2 的结果乘以 5”。在这两种情况下,如果这是您想要的操作,则需要添加括号。