【发布时间】:2020-12-03 00:28:14
【问题描述】:
我是 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’ is applied to too little arguments”,但我不明白是什么原因造成的,因为同样的表达式也有效。
【问题讨论】:
-
2 : tail $ tail test等价于(2 : tail) (tail test),而不是2 : (tail (tail test)) -
@WillemVanOnsem:成功了!谢谢你。正如我在另一个答案中提到的,我确实尝试了
2 : $ tail $ tail test,它返回了parse error on input ‘$’。你知道这是为什么吗?