Sunday, June 7, 2009

Ex-2.19

(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)))))
(define (first-denomination coins)
(car coins))
(define (no-more? coin-values)
(= (length coin-values) 0))
(define (except-first-denomination coin-values)
(cdr coin-values))

Order of coin-values doesn't affect the answer produced, because in the cc we're treating coin-values as just a set of values without any assumption of its order.

No comments:

Post a Comment