# Exercise 2.19 of SICP

Exercise 2.19: Consider the change-counting program of section 1.2.2. It would be nice to be able to easily change the currency used by the program, so that we could compute the number of ways to change a British pound, for example. As the program is written, the knowledge of the currency is distributed partly into the procedure first-denomination and partly into the procedure count-change (which knows that there are five kinds of U.S. coins) . It would be nicer to be able to supply a list of coins to be used for making change. We want to rewrite the procedure cc so that its second argument is a list of the values of the coins to use rather than an integer specifying which coins to use. We could then have lists that defined each kind of currency:

(define us-coins (list 50 25 10 5 1))
(define uk-coins (list 100 50 20 10 5 2 1 0.5))


We could then call cc as follows:

(cc 100 us-coins)
292


To do this will require changing the program cc somewhat. It will still have the same form, but it will access its second argument differently, as follows:

(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 the procedures first-denomination, except-first-denomination, and no-more? in terms of primitive operations on list structures. Does the order of the list coin-values affect the answer produced by cc? Why or why not? I wanted to use lexical scope so the whole program is bellow:

(define (cc amount coin-values)
(define (no-more? coin-values)
(null? coin-values))
(define (first-denomination coin-values)
(car coin-values))
(define (except-first-denomination coin-values)
(cdr 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 us-coins (list 50 25 10 5 1))
(define uk-coins (list 100 50 20 10 5 2 1 0.5))

> (cc 100 us-coins)
292
> (cc 100 uk-coins)
104561
> (cc 100 (list 50 5 25 1 10))
292
> (cc 100 (list 50 2 10 5 100 1 0.5 20))
104561


The order is irrelevant as long as all the coin values are in the list. This is true by looking and what the two recursion branches do:

Branch 1:

(cc amount (except-first-denomination coin-values))

will always bottom out. As the matter of fact, this branch doesn’t even care if coin-values are numbers let alone what order they are in! As long as the (length coin-values) is the right number (in the case of us-coins that number is 5) it will execute without complaining.

Branch 2:

(cc (- amount (first-denomination coin-values)) coin-values)

this branch will subtract every term in coin-values until the amount less that or equal to 0 or coin-values is empty. The reason order is irrelevant is because coupled with the first branch, this second branch makes sure that all sublists of coin-values is evaluated. While the order doesn’t matter in the final result of computation it does matter in the “shape” of the computational process. For instance (cc 51 (list 1 50)) has twice the amount of nodes as (cc 51 (list 50 1))