Skip to content

Instantly share code, notes, and snippets.

@mindbat
Last active Dec 21, 2015
Embed
What would you like to do?
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