Remove the first and the last element from a list in Haskell
up vote
6
down vote
favorite
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.
list haskell
add a comment |
up vote
6
down vote
favorite
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.
list haskell
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
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.
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.
list haskell
list haskell
edited Mar 29 at 22:24
Mateusz Piotrowski
3,69163049
3,69163049
asked Nov 6 '13 at 0:52
Nob Wong
94311
94311
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
14
down vote
accepted
Why not just this?
firstLast::[a]->[a]
firstLast =
firstLast [x] =
firstLast xs = tail (init xs)
Yes.. I simply forgot there is a function called init.. THanks!
– Nob Wong
Nov 6 '13 at 1:09
2
Simplified tofirstLast xs@(_:_) = tail (init xs); firstLast _ =
– jozefg
Nov 6 '13 at 3:15
@jozefg subst "simplified" with "shorter, more complicated, and incorrect". :) You of course meantxs@(_:_:_)
. "simpler" means "more self-evidently correct".
– Will Ness
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
14
down vote
accepted
Why not just this?
firstLast::[a]->[a]
firstLast =
firstLast [x] =
firstLast xs = tail (init xs)
Yes.. I simply forgot there is a function called init.. THanks!
– Nob Wong
Nov 6 '13 at 1:09
2
Simplified tofirstLast xs@(_:_) = tail (init xs); firstLast _ =
– jozefg
Nov 6 '13 at 3:15
@jozefg subst "simplified" with "shorter, more complicated, and incorrect". :) You of course meantxs@(_:_:_)
. "simpler" means "more self-evidently correct".
– Will Ness
yesterday
add a comment |
up vote
14
down vote
accepted
Why not just this?
firstLast::[a]->[a]
firstLast =
firstLast [x] =
firstLast xs = tail (init xs)
Yes.. I simply forgot there is a function called init.. THanks!
– Nob Wong
Nov 6 '13 at 1:09
2
Simplified tofirstLast xs@(_:_) = tail (init xs); firstLast _ =
– jozefg
Nov 6 '13 at 3:15
@jozefg subst "simplified" with "shorter, more complicated, and incorrect". :) You of course meantxs@(_:_:_)
. "simpler" means "more self-evidently correct".
– Will Ness
yesterday
add a comment |
up vote
14
down vote
accepted
up vote
14
down vote
accepted
Why not just this?
firstLast::[a]->[a]
firstLast =
firstLast [x] =
firstLast xs = tail (init xs)
Why not just this?
firstLast::[a]->[a]
firstLast =
firstLast [x] =
firstLast xs = tail (init xs)
answered Nov 6 '13 at 1:02
Benten
708814
708814
Yes.. I simply forgot there is a function called init.. THanks!
– Nob Wong
Nov 6 '13 at 1:09
2
Simplified tofirstLast xs@(_:_) = tail (init xs); firstLast _ =
– jozefg
Nov 6 '13 at 3:15
@jozefg subst "simplified" with "shorter, more complicated, and incorrect". :) You of course meantxs@(_:_:_)
. "simpler" means "more self-evidently correct".
– Will Ness
yesterday
add a comment |
Yes.. I simply forgot there is a function called init.. THanks!
– Nob Wong
Nov 6 '13 at 1:09
2
Simplified tofirstLast xs@(_:_) = tail (init xs); firstLast _ =
– jozefg
Nov 6 '13 at 3:15
@jozefg subst "simplified" with "shorter, more complicated, and incorrect". :) You of course meantxs@(_:_:_)
. "simpler" means "more self-evidently correct".
– Will Ness
yesterday
Yes.. I simply forgot there is a function called init.. THanks!
– Nob Wong
Nov 6 '13 at 1:09
Yes.. I simply forgot there is a function called init.. THanks!
– Nob Wong
Nov 6 '13 at 1:09
2
2
Simplified to
firstLast xs@(_:_) = tail (init xs); firstLast _ =
– jozefg
Nov 6 '13 at 3:15
Simplified to
firstLast xs@(_:_) = tail (init xs); firstLast _ =
– jozefg
Nov 6 '13 at 3:15
@jozefg subst "simplified" with "shorter, more complicated, and incorrect". :) You of course meant
xs@(_:_:_)
. "simpler" means "more self-evidently correct".– Will Ness
yesterday
@jozefg subst "simplified" with "shorter, more complicated, and incorrect". :) You of course meant
xs@(_:_:_)
. "simpler" means "more self-evidently correct".– Will Ness
yesterday
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19802039%2fremove-the-first-and-the-last-element-from-a-list-in-haskell%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password