Example: factorial [002M]
As another example, let's implement factorial (although it is already built in). Recall that n! = n \cdot (n-1) \cdot (n-2) \cdot \dots \cdot 1. Another way to write this is n! = n \cdot (n-1)! If we add a "base case" 0! = 1, this becomes a perfectly viable way to define factorial. We could transcribe it into Disco as follows:
fac : N -> N fac(0) = 1 fac(n) = n * fac (n-1)Unfortunately, this is a type error. The reason is that since the input to
fac is supposed to be a natural number, that means
n-1 must be a natural number, but it cannot be since it uses
subtraction. We can use the .- operator instead, which works
on natural numbers (you will explore what this operator does on a
homework assignment).
fac : N -> N fac(0) = 1 fac(n) = n * fac (n .- 1)