Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 在列表中使用带有tail of tail的cons会抛出错误_Haskell_Cons - Fatal编程技术网

Haskell 在列表中使用带有tail of tail的cons会抛出错误

Haskell 在列表中使用带有tail of tail的cons会抛出错误,haskell,cons,Haskell,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’

我是一名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>
Prelude>测试
[2,3,4,1]
序曲>尾$tail测试
[4,1]
序曲>2:[4,1]
[2,4,1]
序曲>2:尾$tail测试
:27:1:错误:
•无法匹配预期的类型“[Integer]->t”
实际类型为“[Integer]”
•($)的第一个参数取一个参数,
但是它的类型“[Integer]”没有
在表达式中:2:tail$tail test
在“it”的方程式中:it=2:tail$tail test
•相关绑定包括it::t(绑定时间:27:1)
:27:5:错误:
•无法匹配预期的类型“[Integer]”
实际类型为“[a0]->[a0]”
•可能原因:“tail”应用于太少的参数
在“(:)”的第二个参数中,即“tail”
在表达式中:2:tail
在表达式中:2:tail$tail test
序曲>

我看到错误消息“可能的原因:'tail'应用于太少的参数”,但我不太明白是什么导致了它,因为同一个表达式刚刚工作。

您需要注意运算符分组/优先级:
2:tail$tail test
与“cons 2到值
tail$tail test
”任何大于
5*3+2
的值都是“将
3+2
的结果乘以5”。在这两种情况下,如果这是您想要的操作,您需要添加括号。

2:tail$tail test
等同于
(2:tail)(tail test)
,而不是
2:(tail test))
@WillemVanOnsem:这很有效!谢谢。正如我在另一个答案中提到的,我确实尝试了
2:$tail$tail test
,它在输入“$”时返回了
解析错误。
。你知道这是为什么吗?啊!所以我实际上已经尝试过:
2:$tail$tail test
,但也失败了。我的理解是,$论题是等价的吗?放括号是有效的!谢谢你的回复。如果你能解释一下为什么
$
会起作用,那将是令人惊讶的!
$
并不等价于括号,而是避免使用括号的一种方法,因为
$
的优先级比(几乎)低任何其他操作符。@Yathi
2:$tail$tail test
不起作用的原因与
5*+3+2
不起作用的原因完全相同。@Yathi,但
(2:)$tail$tail test
(:)2$tail$tail test
都能起作用。
(:)
是与
操作符相对应的实际函数,
(2:)
(:)2
\x->2:x
(我不记得是哪一个,而且很少有关系)。