Blob


1 (define (fib n)
2 (cond ((= n 0) 0)
3 ((= n 1) 1)
4 (else (+ (fib (- n 1))
5 (fib (- n 2))))))
7 (define (fib n)
8 (fib-iter 1 0 n))
9 (define (fib-iter a b count)
10 (if (= count 0)
11 b
12 (fib-iter (+ a b) a (- count 1))))
14 (define (count-change cents coins)
15 (cond ((= coins 0) 0)
16 ((< cents 0) 0)
17 ((= cents 0) 1)
18 (else (+ (count-change cents (- coins 1))
19 (count-change (- cents (largest-coin-value coins)) coins)))))
21 (define (largest-coin-value coins)
22 (cond ((= coins 5) 50)
23 ((= coins 4) 25)
24 ((= coins 3) 10)
25 ((= coins 2) 5)
26 ((= coins 1) 1)))
28 (count-change 0 0)
29 ;; 0
30 (count-change 1 0)
31 ;; 0
32 (count-change 0 1)
33 ;; 1
34 (count-change 1 1)
35 ;; 1
36 (count-change 2 1)
37 ;; 1
38 (count-change 2 2)
39 ;; 1
40 (count-change 100 5)
42 (define (count-change amount)
43 (cc amount 5))
44 (define (cc amount kinds-of-coins)
45 (cond ((= amount 0) 1)
46 ((or (< amount 0) (= kinds-of-coins 0)) 0)
47 (else (+ (cc amount
48 (- kinds-of-coins 1))
49 (cc (- amount
50 (first-denomination kinds-of-coins))
51 kinds-of-coins)))))
52 (define (first-denomination kinds-of-coins)
53 (cond ((= kinds-of-coins 1) 1)
54 ((= kinds-of-coins 2) 5)
55 ((= kinds-of-coins 3) 10)
56 ((= kinds-of-coins 4) 25)
57 ((= kinds-of-coins 5) 50)))