;; We define our abstraction for zero
(define zero 'D)
;; Increment a number, i.e. get its successor
(define (inc number)
(cons 'P number))
;; Decrement a number, i.e. get its predecessor
(define (dec number)
(cdr number))
;; Adding two numbers is done by "shifting" from one to the other, one by one.
;; a + b = (a + 1) + (b - 1)
(define (add lhs rhs)
(if (eq? rhs zero)
lhs
(add (inc lhs) (dec rhs))))
;; Multiplying the dumb way:
;; a * b = a + (a * (b - 1))
(define (mul lhs rhs)
(if (or (eq? rhs zero) (eq? lhs zero))
zero
(if (eq? rhs (inc zero))
lhs
(add lhs (mul lhs (dec rhs))))))
;; Some values to play with
(define one (inc zero))
(define two (inc one))
(define three (inc two))
(define four (inc three))
;; Display results
(display one)(newline)
(display two)(newline)
(display (add one two))(newline)
(display (mul two three))(newline)
(display (mul three (mul three three)))(newline)
OzsgV2UgZGVmaW5lIG91ciBhYnN0cmFjdGlvbiBmb3IgemVybwooZGVmaW5lIHplcm8gJ0QpCgo7OyBJbmNyZW1lbnQgYSBudW1iZXIsIGkuZS4gZ2V0IGl0cyBzdWNjZXNzb3IKKGRlZmluZSAoaW5jIG51bWJlcikKCShjb25zICdQIG51bWJlcikpCgo7OyBEZWNyZW1lbnQgYSBudW1iZXIsIGkuZS4gZ2V0IGl0cyBwcmVkZWNlc3NvcgooZGVmaW5lIChkZWMgbnVtYmVyKQoJKGNkciBudW1iZXIpKQoKOzsgQWRkaW5nIHR3byBudW1iZXJzIGlzIGRvbmUgYnkgInNoaWZ0aW5nIiBmcm9tIG9uZSB0byB0aGUgb3RoZXIsIG9uZSBieSBvbmUuCjs7IGEgKyBiID0gKGEgKyAxKSArIChiIC0gMSkKKGRlZmluZSAoYWRkIGxocyByaHMpCgkoaWYgKGVxPyByaHMgemVybykKCQlsaHMKCQkoYWRkIChpbmMgbGhzKSAoZGVjIHJocykpKSkKCjs7IE11bHRpcGx5aW5nIHRoZSBkdW1iIHdheToKOzsgYSAqIGIgPSBhICsgKGEgKiAoYiAtIDEpKQooZGVmaW5lIChtdWwgbG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