Last active
December 21, 2015 08:18
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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