[Haskell-cafe] Trouble with non-exhaustive patterns

Fernando Rodriguez frr149 at easyjob.net
Mon Jul 21 07:09:29 EDT 2008


I defiend the  following function to get the last element of a list:

final [a] = a
final (_:t) = final t

and it works as expected. Since I didn't want to have a non exhaustive pattern, 
I added the following case:

final []  = [] - I consider that the end of an empty list is the empty list
final [a] = a
final (_:t) = final t

Suddenly, the function stoped working with a rather cryptic (for a newbie 
at least) error message:

*Temp> final [4,5]

    No instance for (Num [a])
      arising from the literal `5' at <interactive>:1:9
    Possible fix: add an instance declaration for (Num [a])
    In the expr*Temp> ession: 5
    In the first argument of `final', namely `[4, 5]'
    In the expression: final [4, 5]

What have I done so wrong?

Thanks in advance,

More information about the Haskell-Cafe mailing list