-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex1.11.scm
36 lines (31 loc) · 806 Bytes
/
ex1.11.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
;; recursive process
(define (f n)
(cond ((< n 3) n)
(else
(+
(f (- n 1))
(* 2 (f (- n 2)))
(* 3 (f (- n 3)))))))
(define f-recur f)
;; iterative process
;; uses an accumulator `accu` to know when done
(define (f n)
(define (f-iter n fn-1 fn-2 fn-3 accu)
(cond ((< n 3) n)
((= accu n)
(+ fn-1 (* 2 fn-2) (* 3 fn-3)))
(else
(f-iter n
(+ fn-1 (* 2 fn-2) (* 3 fn-3))
fn-1
fn-2
(+ 1 accu)))))
(f-iter n 2 1 0 3))
(define f-iter f)
(define (check-f-up-to n)
(cond ((< n 3) (= (f-recur n) (f-iter n)))
(else
(and (= (f-recur n) (f-iter n))
(check-f-up-to (- n 1))))))
(check-f-up-to 100)
;Value: #t