Recursive
Some prossible methods to implement operator+.
prerequisites
There exists some primitive procedures, which are implemented in other method finally.
1
2
3
4
| ; 1-
(define (1- x) ...)
; 1+
(define (1+ x) ...)
|
eg.1 operator+
1
2
3
4
5
6
|
; provided that x is non-negative integer
(define (+ x y)
if (= 0 x)
x
(+ (1- x) (1+ y)))
|
an instance
inspect the compute procedure:
1
2
3
4
5
6
7
8
| (+ 3 4)
(+ (1- 3) (1+ 4))
(+ 2, 5)
(+ (1- 2) (1+ 5))
(+ 1, 6)
(+ (1- 1) (1+ 6))
(+ 0, 7)
7
|
eg.2 fibonacci
1
2
3
4
5
6
| (define (fib x)
(if (< x 3)
1
(+ (fib (- x 1)) (fib (- x 2)))
)
)
|
duplicate compute procedure fib(- x 2)