From 28699c95a7de284f07a5c0e34fb1755868301f3c Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 9 Dec 2011 18:10:41 -0400 Subject: some work on avoiding partial functions There are still hundreds of places that use partial functions head, tail, init, and last. --- Utility/BadPrelude.hs | 24 ++++++++++++++++++++++++ Utility/Misc.hs | 13 +++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 Utility/BadPrelude.hs (limited to 'Utility') diff --git a/Utility/BadPrelude.hs b/Utility/BadPrelude.hs new file mode 100644 index 000000000..1bb70adfb --- /dev/null +++ b/Utility/BadPrelude.hs @@ -0,0 +1,24 @@ +{- Some stuff from Prelude should not be used, as it tends to be a source + - of bugs. + - + - This exports functions that conflict with the prelude, which avoids + - them being accidentially used. + -} + +module Utility.BadPrelude where + +{- head is a partial function; head [] is an error -} +head :: [a] -> a +head = Prelude.head + +{- tail is also partial -} +tail :: [a] -> a +tail = Prelude.tail + +{- init too -} +init :: [a] -> a +init = Prelude.init + +{- last too -} +last :: [a] -> a +last = Prelude.last diff --git a/Utility/Misc.hs b/Utility/Misc.hs index 728598723..541e150b7 100644 --- a/Utility/Misc.hs +++ b/Utility/Misc.hs @@ -27,6 +27,19 @@ readMaybe s = case reads s of ((x,_):_) -> Just x _ -> Nothing +{- Like break, but the character matching the condition is not included + - in the second result list. + - + - separate (== ':') "foo:bar" = ("foo", "bar") + - separate (== ':') "foobar" = ("foo, "") + -} +separate :: (a -> Bool) -> [a] -> ([a], [a]) +separate c l = unbreak $ break c l + where + unbreak r@(a, b) + | null b = r + | otherwise = (a, tail b) + {- Catches IO errors and returns a Bool -} catchBoolIO :: IO Bool -> IO Bool catchBoolIO a = catchDefaultIO a False -- cgit v1.2.3