Exercise: A module for Peano arithmetic
Write a module1 peano which implements the following functions:
| Name and type | Description |
|---|---|
zero :: -> peano |
Constructor for the zero of the system, i.e., returns None |
succ :: peano -> peano |
Constructor for the rest |
zerop :: peano -> bool |
Check whether the given peano object is zero (in the peano sense).2 |
peanop:: obj -> bool |
Check whether the argument is peano. |
to_int :: peano -> int |
Conversion |
from_int :: int -> peano |
ditto |
peano_add :: peano, peano -> peano |
Addition |
peano_mult :: peano, peano -> peano |
Multiplication |
peano_eq :: peano, peano -> bool |
Equality |
Here are the rules of the game:
- All your functions must be recursively defined (no
while,proc, whatever). - No use of
+,-,==,<=, and so on, in your definitions. As long as it is not numerical comparison,==is allowed.
-
If we are not already in the module business, “write a module” means create a
peano.pyfile and define your functions there. Later on you can import your function names withfrom peano import peano_add, peano_mult, and so on. ↩ -
We follow the
LISPconvention of naming predicates by attaching apat the end of the corresponding property. The programmatic way to inquire about someone’s mood is to applyhappypto him/her. ↩