This Haskell code prints out [0,10,20,30,40,50] but I don’t understand what the ‘y’ is suppose to do in the third line.
f [] = []
f [x] = [x]
f (x:y:xs) = x : f xs
main = print (f [0,5..50])
Why doesn’t it print the same result if I say f (x:xs) = x : f xs instead ?
I’d really appreciate it if someone could explain the logic for me.
>Solution :
The pattern in f (x:y:xs) is saying: get the input to the function, assign the first element of the list to x, the second to y and the rest (tail) of the list to xs. And this function is returning the first element x followed by the result of applying f to xs. In essence, you are removing every second element from the list.