Fastest way to get the last element of a list in Haskell

Dilawar picture Dilawar · Sep 11, 2011 · Viewed 57.9k times · Source

What is the fastest way to get the last element of a list in Haskell. Also in next iteration, I want to remove first and last element of the list. What is the most elegant way to do it? I am trying list comprehension, but that does not look very efficient!

Answer

icktoofay picture icktoofay · Sep 11, 2011

You can use the last function to get the last element of a list.

As for how to remove the first and last elements, you could use (init . tail), but I don't know how efficient that is.

I think this image from Learn You A Haskell shows the list functions fairly well:

illustration of list functions