Skip to content

Instantly share code, notes, and snippets.

@pedrofurla
Created October 2, 2021 17:47
Show Gist options
  • Save pedrofurla/ffc61559d7a4fba74d4c42c6da0c3832 to your computer and use it in GitHub Desktop.
Save pedrofurla/ffc61559d7a4fba74d4c42c6da0c3832 to your computer and use it in GitHub Desktop.
λ> :{
> second (x:y:xs) = y : second xs -- returns every second element of a list
> second _ = []
>
> xs = [1,2,3,4] ++ second xs
> :}
λ> second [1,2,3,4]
[2,4]
λ> second $ second [1,2,3,4]
[4]
λ> second $ second $ second [1,2,3,4]
[]
λ> second $ second $ second $ second [1,2,3,4]
[]
-- we can repeat this ad nauseam
@pedrofurla
Copy link
Author

xs = [1,2,3,4] ++ second xs
xs = [1,2,3,4] ++ second ([1,2,3,4] ++ second xs)
xs = [1,2,3,4] ++ second ([1,2,3,4] ++ second ([1,2,3,4] ++ second ([1,2,3,4] ++ second xs)))
xs = [1,2,3,4] ++ second ([1,2,3,4] ++ second ([1,2,3,4] ++ second ([1,2,3,4] ++ second ([1,2,3,4] ++ second ([1,2,3,4] ++ second ([1,2,3,4] ++ second ([1,2,3,4] ++ second xs)))))))

@pedrofurla
Copy link
Author

It just occurred to me. The ideal example is xs = [] ++ second xs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment