summaryrefslogtreecommitdiff
path: root/Utility
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2011-12-15 18:11:42 -0400
committerGravatar Joey Hess <joey@kitenet.net>2011-12-15 18:19:36 -0400
commit95d2391f58ae240e7100f0d5488dd7246f71f3bb (patch)
treef33f21904ae7be4d40b70bab1e2a68fd4eef5526 /Utility
parentb7e0d39abbc9a09c21c6f0103ad6c9f4547f81fe (diff)
more partial function removal
Left a few Prelude.head's in where it was checked not null and too hard to remove, etc.
Diffstat (limited to 'Utility')
-rw-r--r--Utility/BadPrelude.hs14
1 files changed, 12 insertions, 2 deletions
diff --git a/Utility/BadPrelude.hs b/Utility/BadPrelude.hs
index 49837b927..47d38ae7b 100644
--- a/Utility/BadPrelude.hs
+++ b/Utility/BadPrelude.hs
@@ -12,7 +12,7 @@ read :: Read a => String -> a
read = Prelude.read
{- head is a partial function; head [] is an error
- - Instead, use: take 1 -}
+ - Instead, use: take 1 or headMaybe -}
head :: [a] -> a
head = Prelude.head
@@ -27,10 +27,20 @@ init :: [a] -> [a]
init = Prelude.init
{- last too
- - Instead, use: end -}
+ - Instead, use: end or lastMaybe -}
last :: [a] -> a
last = Prelude.last
+{- Like head but Nothing on empty list. -}
+headMaybe :: [a] -> Maybe a
+headMaybe [] = Nothing
+headMaybe v = Just $ Prelude.head v
+
+{- Like last but Nothing on empty list. -}
+lastMaybe :: [a] -> Maybe a
+lastMaybe [] = Nothing
+lastMaybe v = Just $ Prelude.last v
+
{- All but the last element of a list.
- (Like init, but no error on an empty list.) -}
beginning :: [a] -> [a]