Avoiding partial functions
From HaskellWiki
(Difference between revisions)
(minimum) |
(→fromJust) |
||
| (3 intermediate revisions not shown.) | |||
| Line 20: | Line 20: | ||
Nothing -> g | Nothing -> g | ||
Just x -> h x | Just x -> h x | ||
| + | </haskell> | ||
| + | which is equivalent to | ||
| + | <haskell> | ||
| + | maybe g h mx | ||
</haskell> | </haskell> | ||
| Line 62: | Line 66: | ||
forcePair ~(a,b) = (a,b) | forcePair ~(a,b) = (a,b) | ||
</haskell> | </haskell> | ||
| + | |||
| + | You might define <hask>viewRTotal</hask> in a <code>Utility</code> module. | ||
| + | You can import <hask>forcePair</hask> from [http://hackage.haskell.org/packages/archive/utility-ht/0.0.8/doc/html/Data-Tuple-HT.html utility-ht]. | ||
== (!!) == | == (!!) == | ||
| Line 115: | Line 122: | ||
Thus it is also faster for finite lists. | Thus it is also faster for finite lists. | ||
Even more: It also works for empty lists. | Even more: It also works for empty lists. | ||
| + | |||
| + | |||
| + | [[Category:Tutorials]] | ||
| + | [[Category:Style]] | ||
| + | [[Category:Idioms]] | ||
Current revision
There are several partial functions in the Haskell standard library. If you use them, you always risk to end up with an undefined. In this article we give some hints how to avoid them, leading to code that you can be more confident about.
For a partial function f the general pattern is: Whereever we write "check whether x is in the domain of f before computing f x", we replace it by combination of check and computation of f.
Contents |
1 fromJust
You should replace
if isNothing mx then g else h (fromJust mx)
by
case mx of Nothing -> g Just x -> h x
which is equivalent to
maybe g h mx2 head, tail
You should replace
if null xs then g else h (head xs) (tail xs)
by
case xs of [] -> g y:ys -> h y ys
3 init, last
You may replace
if null xs then g else h (init xs) (last xs)
by
case xs of [] -> g y:ys -> uncurry h $ viewRTotal y ys viewRTotal :: a -> [a] -> ([a], a) viewRTotal x xs = forcePair $ foldr (\x0 go y -> case go y of ~(zs,z) -> (x0:zs,z)) (\y -> ([],y)) xs x forcePair :: (a,b) -> (a,b) forcePair ~(a,b) = (a,b)
viewRTotal
Utility module.
You can import forcePair
4 (!!)
You should replace
if k < length xs then xs!!k else y
by
case drop k xs of x:_ -> x [] -> y
length
5 irrefutable pattern match on (:)
You should replace
if k < length xs then let (prefix,x:suffix) = splitAt k xs in g prefix x suffix else y
by
case splitAt k xs of (prefix,x:suffix) -> g prefix x suffix (_,[]) -> y
6 minimum
The functionisLowerLimit
minimum
isLowerLimit :: Ord a => a -> [a] -> Bool isLowerLimit x ys = x <= minimum ys
ys
You should replace it by
isLowerLimit x = all (x<=)
x
x
Thus it is also faster for finite lists. Even more: It also works for empty lists.
Categories: Tutorials | Style | Idioms
