Bear with me as I am new to Haskell.
I'm trying to write a function that outputs:
The first list in the output should be the same as the input list. The second list in the output should contain every second element from the input list and so on.
example: func "ABCD" = ["ABCD","BD","C","D"]
I started by writing a function to get every nth element of a list:
everyN n xs = case drop (n-1) xs of
[] -> []
(z:zs) -> z : everyN n zs
Now I know that I just need to have a function which calls this function and increments the n
value each time it is called, but I can't for the life of my think about how to write such a function or what would be the most concise way to express it. If possible I'd like to not use explicit recursion and use a haskell language function.
Can someone point me in the right direction?
[everyN n "ABCDEFGHI" | n <- [1..9]]
. Alternatively, usemap
instead of list comprehensions. – chi[ .. ]
) where we generate the items of this list byeveryN n xs
and we taken
from1..9
– Random Dev