Skip to content

Latest commit

 

History

History
58 lines (44 loc) · 1.48 KB

subtraction.rst

File metadata and controls

58 lines (44 loc) · 1.48 KB

Subtraction

Integers <integer> and rational numbers <rational> can be subtracted using the - operator. For example:

Disco> 1 - 5
-4
Disco> 2/3 - 1/2
1/6

It is not possible to subtract natural numbers <natural> or fractional numbers <fraction> since those numeric types <numeric> have no concept of negative numbers; however, in many situations, if you subtract natural or fractional numbers they will be automatically converted to integers or rationals as appropriate.

In some situations, you really do need to subtract natural or fractional numbers. For example, consider this incorrect definition of the factorial function:

fact_bad : N -> N
fact_bad(0) = 1
fact_bad(n) = n * fact_bad(n-1)

This definition will yield an error, because subtracting two natural numbers is not allowed: in particular, n is a natural number and we are attempting to perform the subtraction n-1. One solution is to use the special "dot minus" operator .- (also written ), which simply stops at zero instead of yielding negative numbers:

Disco> 5 .- 3
2
Disco> 5 .- 4
1
Disco> 5 .- 5
0
Disco> 5 .- 6
0
Disco> 5 .- 7
0

Using dot minus, we can write a correct definition of factorial as follows:

fact : N -> N
fact(0) = 1
fact(n) = n * fact(n .- 1)

(Alternatively, we could define fact using an arithmetic pattern <arithmeticpat>; see that page for more info.)