The natural numbers can be defined recursively as follows:
- 0 is a natural number.
- if
$n$ is a natural number, then so is the successor of$n$
We can easily represent this in OCaml using corresponding constructors:
type nat = Zero | Succ of natImplement the following functions for natural numbers:
First convert between integers and naturals in the file conv.ml
- int_to_nat
int_to_nat : int -> natconverts an integer to natural. - nat_to_int
nat_to_int : nat -> intconverts a natural to integer.
Then implement some operations on naturals in the file ops.ml:
-
add
add : nat -> nat -> natadds two natural numbers. -
mul
mul : nat -> nat -> natmultiplies two natural numbers. -
pow
pow : nat -> nat -> nata callpow a bcomputes$a^b$ . -
leq
leq : nat -> nat -> boola callleq a bcomputes$a\leq b$
You are not allowed to use integers to implement the operations on naturals!