Skip to content

Instantly share code, notes, and snippets.

@mindbat
Last active December 21, 2015 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindbat/6276874 to your computer and use it in GitHub Desktop.
Save mindbat/6276874 to your computer and use it in GitHub Desktop.
Versions of the head, tail, etc functions that are safe to use on empty lists
safeHead :: [a] -> [a]
safeHead x = take 1 x
safeTail :: [a] -> [a]
safeTail x = drop 1 x
safeInit :: [a] -> [a]
safeInit x = take ((length x) - 1) x
safeLast :: [a] -> [a]
safeLast x = drop ((length x) - 1) x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment