very simple haskell question

I am just starting Haskell and trying to write an alternative function for returning the last element of a list by using the !! function. I have tried...

mylast xs = xs !! (length [xs] - 1)

but this always returns the head of the list. What am I doing wrong?

I did say it was very simple...
like the author.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Better forums for this type of question

Chad, you may have better luck with questions like this in the future on

In short, the answer to your question is that [xs] is a list with one elements, namely xs. You want ((length xs) - 1), but you have ((length [xs]) - 1) = ((1) - 1) = 0.


... on both counts. I haven't tried Stack Overflow yet, though it's very popular and it looks pretty good. Another alternative is to come to the Haskell IRC channel, which is a wonderful place to get simple questions like this answered, 24/7!

newsgroups as well

comp.lang.haskell seems friendly enough.