Difference between revisions of "Avoiding partial functions"
(→fromJust: isNothing) 
(Add reference to the safe library.) 

(7 intermediate revisions by 4 users not shown)  
Line 2:  Line 2:  
If you use them, you always risk to end up with an undefined. 
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. 
In this article we give some hints how to avoid them, leading to code that you can be more confident about. 

+  
+  In addition to the below rewrites, many partial functions can be avoided by using variants from the [https://hackage.haskell.org/package/safe safe] library. 

For a partial function f the general pattern is: 
For a partial function f the general pattern is: 

−  +  Wherever 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. 
we replace it by combination of check and computation of f. 

Line 20:  Line 22:  
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 68:  
forcePair ~(a,b) = (a,b) 
forcePair ~(a,b) = (a,b) 

</haskell> 
</haskell> 

+  
+  Alternatively, you may import from {{HackagePackageid=utilityht}}: 

+  * [http://hackage.haskell.org/packages/archive/utilityht/0.0.8/doc/html/DataTupleHT.html forcePair] 

+  * [http://hackage.haskell.org/packages/archive/utilityht/0.0.8/doc/html/DataListHT.html viewR] 

+  * [http://hackage.haskell.org/packages/archive/utilityht/0.0.8/doc/html/DataListHT.html switchR]. 

== (!!) == 
== (!!) == 

Line 96:  Line 107:  
(_,[]) > y 
(_,[]) > y 

</haskell> 
</haskell> 

+  
+  
+  == minimum == 

+  
+  The function <hask>isLowerLimit</hask> checks if a number is a lower limit to a sequence. 

+  You may implement it with the partial function <hask>minimum</hask>. 

+  <haskell> 

+  isLowerLimit :: Ord a => a > [a] > Bool 

+  isLowerLimit x ys = x <= minimum ys 

+  </haskell> 

+  It fails if <hask>ys</hask> is empty or infinite. 

+  
+  You should replace it by 

+  <haskell> 

+  isLowerLimit x = all (x<=) 

+  </haskell> 

+  This definition terminates for infinite lists, if <hask>x</hask> is not a lower limit. It aborts immediately if an element is found which is below <hask>x</hask>. 

+  Thus it is also faster for finite lists. 

+  Even more: It also works for empty lists. 

+  
+  
+  [[Category:Tutorials]] 

+  [[Category:Style]] 

+  [[Category:Idioms]] 
Latest revision as of 04:03, 4 September 2017
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.
In addition to the below rewrites, many partial functions can be avoided by using variants from the safe library.
For a partial function f the general pattern is: Wherever 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.
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 mx
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
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)
Alternatively, you may import from utilityht:
(!!)
You should replace
if k < length xs
then xs!!k
else y
by
case drop k xs of
x:_ > x
[] > y
This is also more lazy, since for computation of length
you have to visit every element of the list.
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
minimum
The function isLowerLimit
checks if a number is a lower limit to a sequence.
You may implement it with the partial function minimum
.
isLowerLimit :: Ord a => a > [a] > Bool
isLowerLimit x ys = x <= minimum ys
It fails if ys
is empty or infinite.
You should replace it by
isLowerLimit x = all (x<=)
This definition terminates for infinite lists, if x
is not a lower limit. It aborts immediately if an element is found which is below x
.
Thus it is also faster for finite lists.
Even more: It also works for empty lists.