January 2018
Beginner to intermediate
312 pages
7h 22m
English
A mathematical function links each possible input to an output. In functional programming we try to design our functions the same way, so that every input has a corresponding output. These kinds of functions are called total functions.
Why bother? Because we want to make things explicit as much as possible, with all effects documented in the type signature.
Let’s demonstrate the concept with a rather silly function, twelveDividedBy, which returns the result of 12 divided by the input using integer division. In pseudo-code, we could implement this with a table of cases, like this:
| | let twelveDividedBy n = |
| | match n with |
| | | 6 -> 2 |
| | | 5 -> 2 |
| | | 4 -> 3 |
| | | 3 -> 4 |
| | | 2 -> 6 |
| | | 1 -> 12 |
| | | 0 -> ??? |
Now what should the answer ...
Read now
Unlock full access