Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell- looping every second element of list

I want to be able to loop every second element of a given list. I can do this recursively as so:

check validate (x:xs) = check (validate x) (tail xs)

But the problem is that I need a function that accepts a list as parameter, then returns a list consisting of only every second element in the list, starting with (and including) the first element of the list, and I do not think this is possible recursively.

Can someone show me how to this using list comprehension? This would probably be the best approach.

like image 819
buydadip Avatar asked Dec 17 '25 17:12

buydadip


1 Answers

second (x:y:xs) = y : second xs;
second _ = []

List comprehension may not be useful.

like image 146
Abhay Avatar answered Dec 20 '25 11:12

Abhay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!