some work on avoiding partial functions

There are still hundreds of places that use partial functions head, tail,
init, and last.
This commit is contained in:
Joey Hess 2011-12-09 18:10:41 -04:00
parent 95e748cbd4
commit 28699c95a7
4 changed files with 45 additions and 14 deletions

View file

@ -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