Friday 29 April 2016

Haskell: Total and Partial functions

If a function is well defined on all possible arguments, then it is called total function, if it is not well defined for all possible arguments, then it is called partial function. There are several partial functions defined in Haskell standard library.

For Example,
    a. head, tail: undefined for empty lists
    b. (!!) : undefined if the index is at least as big as the list length

    c. div : undefined if the divisor is zero

Prelude> head []
*** Exception: Prelude.head: empty list
Prelude> 
Prelude> tail []
*** Exception: Prelude.tail: empty list
Prelude> 
Prelude> 10 `div` 0
*** Exception: divide by zero

Always avoid of defining partial functions in your code.

Following post explains some guide lines to avoid partial functions.




Previous                                                 Next                                                 Home

No comments:

Post a Comment