我刚刚开始学习Haskell,我打算编写一个返回到最后一个列表2的函数。
lastButOne x = if ((==) (length x) 2)
then (head x)
else (tail x)
Run Code Online (Sandbox Code Playgroud)
这是错误的:
? Occurs check: cannot construct the infinite type: a ~ [a]
? In the expression: (tail x)
In the expression:
if ((==) (length x) 2) then (head x) else (tail x)
In an equation for ‘lastButOne’:
lastButOne x = if ((==) (length x) 2) then (head x) else (tail x)
? Relevant bindings include
x :: [a] (bound at D:\\tool\8.6.3\test\lBoErr.hs:1:12)
lastButOne :: [a] -> …Run Code Online (Sandbox Code Playgroud) haskell ×1