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.
Try mylast [] = error "Empty list!"
instead. Otherwise Haskell cannot infer the type of your function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With