Safe Haskell | Safe |
---|---|

Language | Haskell2010 |

Provides functions that raise errors in corner cases instead of returning "best effort" results, then provides wrappers like the Safe module. For example:

raises an error, in contrast to`takeExact`

3 [1,2]`take`

which would return just two elements.

raises an error, in contrast to`takeExact`

(-1) [1,2]`take`

which would return no elements.

raises an error, in contrast to`zip`

[1,2] [1]`zip`

which would only pair up the first element.

Note that the `May`

variants of these functions are *strict* in at least the bit of the prefix
of the list required to spot errors. The standard and `Note`

versions are lazy, but throw
errors later in the process - they do not check upfront.

- takeExact :: Int -> [a] -> [a]
- dropExact :: Int -> [a] -> [a]
- splitAtExact :: Int -> [a] -> ([a], [a])
- zipExact :: [a] -> [b] -> [(a, b)]
- zipWithExact :: (a -> b -> c) -> [a] -> [b] -> [c]
- takeExactMay :: Int -> [a] -> Maybe [a]
- takeExactNote :: String -> Int -> [a] -> [a]
- takeExactDef :: [a] -> Int -> [a] -> [a]
- dropExactMay :: Int -> [a] -> Maybe [a]
- dropExactNote :: String -> Int -> [a] -> [a]
- dropExactDef :: [a] -> Int -> [a] -> [a]
- splitAtExactMay :: Int -> [a] -> Maybe ([a], [a])
- splitAtExactNote :: String -> Int -> [a] -> ([a], [a])
- splitAtExactDef :: ([a], [a]) -> Int -> [a] -> ([a], [a])
- zipExactMay :: [a] -> [b] -> Maybe [(a, b)]
- zipExactNote :: String -> [a] -> [b] -> [(a, b)]
- zipExactDef :: [(a, b)] -> [a] -> [b] -> [(a, b)]
- zipWithExactMay :: (a -> b -> c) -> [a] -> [b] -> Maybe [c]
- zipWithExactNote :: String -> (a -> b -> c) -> [a] -> [b] -> [c]
- zipWithExactDef :: [c] -> (a -> b -> c) -> [a] -> [b] -> [c]

# New functions

takeExact :: Int -> [a] -> [a] Source

takeExact n xs = | n >= 0 && n <= length xs = take n xs | otherwise = error "some message"

dropExact :: Int -> [a] -> [a] Source

dropExact n xs = | n >= 0 && n <= length xs = drop n xs | otherwise = error "some message"

splitAtExact :: Int -> [a] -> ([a], [a]) Source

splitAtExact n xs = | n >= 0 && n <= length xs = splitAt n xs | otherwise = error "some message"

zipExact :: [a] -> [b] -> [(a, b)] Source

zipExact xs ys = | length xs == length ys = zip xs ys | otherwise = error "some message"

zipWithExact :: (a -> b -> c) -> [a] -> [b] -> [c] Source

zipWithExact f xs ys = | length xs == length ys = zipWith f xs ys | otherwise = error "some message"

# Safe wrappers

takeExactMay :: Int -> [a] -> Maybe [a] Source

takeExactNote :: String -> Int -> [a] -> [a] Source

takeExactDef :: [a] -> Int -> [a] -> [a] Source

dropExactMay :: Int -> [a] -> Maybe [a] Source

dropExactNote :: String -> Int -> [a] -> [a] Source

dropExactDef :: [a] -> Int -> [a] -> [a] Source

splitAtExactMay :: Int -> [a] -> Maybe ([a], [a]) Source

splitAtExactNote :: String -> Int -> [a] -> ([a], [a]) Source

splitAtExactDef :: ([a], [a]) -> Int -> [a] -> ([a], [a]) Source

zipExactMay :: [a] -> [b] -> Maybe [(a, b)] Source

zipExactNote :: String -> [a] -> [b] -> [(a, b)] Source

zipExactDef :: [(a, b)] -> [a] -> [b] -> [(a, b)] Source

zipWithExactMay :: (a -> b -> c) -> [a] -> [b] -> Maybe [c] Source

zipWithExactNote :: String -> (a -> b -> c) -> [a] -> [b] -> [c] Source

zipWithExactDef :: [c] -> (a -> b -> c) -> [a] -> [b] -> [c] Source