Skip to content

Instantly share code, notes, and snippets.

@lnikon
Created November 23, 2018 18:05
Show Gist options
  • Save lnikon/2a21f40c77cfc3bd14b903bcb117e703 to your computer and use it in GitHub Desktop.
Save lnikon/2a21f40c77cfc3bd14b903bcb117e703 to your computer and use it in GitHub Desktop.
1) Find the last element of a list.
xs = [1, 2, 3, 4]
last xs
2) Find the last but one element of a list.
xs = [1, 2, 3, 4]
last (init xs)
3) Find the K'th element of a list. The first element in the list is number 1.
xs = [1, 2, 4, 3, 5, 6, 8, 7]
elementAt xs k = if k > 0 then elementAt (tail xs) (k - 1) else (last (reverse xs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment