I follow the real world haskell, and there is an exercise in chapter 2.
My solution is
lastButOne xs = if null xs || null (tail xs)
then []
else if null (tail (tail xs))
then head xs
else lastButOne (tail xs)
but it doesn't work other than [], and produces such an error.
*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]
I am fairly a newbie and don't understand the cryptic error messages. Any ideas?
null,headandtail.