[Haskell-cafe] Trouble with non-exhaustive patterns

Janis Voigtlaender voigt at tcs.inf.tu-dresden.de
Mon Jul 21 07:24:43 EDT 2008


Fernando Rodriguez wrote:
> 
> Hi,
> 
> 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]
> 
> <interactive>:1:9:
>     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?

You probably want final to have type

  final :: [a] -> a

But the equation

  final [] = []

conflicts with this.

BTW, you might want to have this kind of discussion at 
beginners at haskell.org instead. See the announcement:

http://thread.gmane.org/gmane.comp.lang.haskell.general/16345

Ciao, Janis.

-- 
Dr. Janis Voigtlaender
http://wwwtcs.inf.tu-dresden.de/~voigt/
mailto:voigt at tcs.inf.tu-dresden.de


More information about the Haskell-Cafe mailing list