Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing the last function

Tags:

haskell

I am trying to teach myself Haskell. One thing I have tried to do is to write an alternative function to return the last element of a list by recursing through the list until the tail is the empty set and then returning the head. I have...

mylast [] = []
mylast (x:[]) = x
mylast (x:xs) = mylast xs

...but I get an error when I try any non-empty list :( Any suggestions as to what I have done wrong? TIA.

like image 204
user147056 Avatar asked Jul 29 '09 12:07

user147056


1 Answers

Try mylast [] = error "Empty list!" instead. Otherwise Haskell cannot infer the type of your function.

like image 106
EFraim Avatar answered Oct 03 '22 04:10

EFraim