-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathex2.19-change-count-revisit.scm
48 lines (37 loc) · 1.17 KB
/
ex2.19-change-count-revisit.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
36
37
38
39
40
41
42
43
44
45
46
47
48
(define (count-change amount)
(cc1 amount 5))
(define (cc1 amount kinds-of-coins)
(cond ((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)
(else (+ (cc1 amount
(- kinds-of-coins 1))
(cc1 (- amount
(first-denomination kinds-of-coins))
kinds-of-coins)))))
(define (first-denomination kinds-of-coins)
(cond ((= kinds-of-coins 1) 1)
((= kinds-of-coins 2) 5)
((= kinds-of-coins 3) 10)
((= kinds-of-coins 4) 25)
((= kinds-of-coins 5) 50)))
(count-change 100)
(define us-coins (list 50 25 10 5 1))
(define uk-coins (list 100 50 20 10 5 2 1 0.5))
(define first-denomination car)
(define except-first-denomination cdr)
(define no-more? null?)
(define (cc amount coin-values)
(cond ((= amount 0) 1)
((or (< amount 0) (no-more? coin-values)) 0)
(else
(+ (cc amount
(except-first-denomination coin-values))
(cc (- amount
(first-denomination coin-values))
coin-values)))))
(cc 100 us-coins)
; The order doesn't matter because with the existence of condition:
; (cc amount (except-first-denomination coin-values))
; each denomination got the chance to be processed with initial amount
; since cc is resursive, so on so forth
;