我遵循现实世界的haskell,第2章有一个练习。
我的解决方案是
lastButOne xs = if null xs || null (tail xs)
then []
else if null (tail (tail xs))
then head xs
else lastButOne (tail xs)
但是除了[]之外它不起作用,并产生这样的错误。
*Main> lastButOne []
[]
*Main> lastButOne [1, 2]
<interactive>:5:13:
No instance for (Num [a0]) arising from the literal `1'
Possible fix: add an instance declaration for (Num [a0])
In the expression: 1
In the first argument of `lastButOne', namely `[1, 2]'
In the expression: lastButOne [1, 2]
我是一个新手,不理解神秘的错误消息。有任何想法吗?