在列表中使用尾部为尾部的cons会引发错误



我是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"应用于太少的参数"但我还不太明白是什么原因导致的,因为同样的表达方式刚刚奏效。

您需要注意运算符分组/优先级:2 : tail $ tail test与"cons2加到值CCD_ 2"上;任何大于CCD_ 3的值都是"0";将CCD_ 4的结果乘以5";。在这两种情况下,如果需要的话,都需要添加括号。

最新更新