Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the first and the last element from a list in Haskell

Tags:

list

haskell

I am writing a small function which can help me get rid of the first and last element of a list. This is what I am trying to do:

>firstLast::[a]->[a]
>firstLast [x] = [ ]
>firstLast h:t = [i|i!= head[a] || i!= last[a]]

As you can see I am trying to use list comprehension here,but apparently I didn't use it properly.

like image 389
Nob Wong Avatar asked Nov 06 '13 00:11

Nob Wong


1 Answers

Why not just this?

firstLast::[a]->[a]
firstLast [] = []
firstLast [x] = []
firstLast xs = tail (init xs)
like image 141
Benten Avatar answered Nov 14 '22 21:11

Benten