Recurse on the first argument for addition and multiplication, and on the exponent for exponentiation:
add :: Natural -> Natural -> Natural
add Zero n = n
add (Succ m) n = Succ (add m n)
mul :: Natural -> Natural -> Natural
mul Zero n = Zero
mul (Succ m) n = add n (mul m n)
power :: Natural -> Natural -> Natural
power m Zero = Succ Zero
power m (Succ n) = mul m (power m n)The defining arithmetic identities areEach recursive call consumes one constructor. The base of exponentiation makes
power Zero Zero equal to one, the empty-product convention. Applying convert and using structural induction verifies all three operations. Articles by others on the same topic
There are currently no matching articles.