Blob


1 (define (list-ref items n)
2 (if (= n 0)
3 (cat items)
4 (list-ref (cdr items) (- n 1))))
6 (define (length items)
7 (define (length-ter a count)
8 (if (null? a)
9 count
10 (length-iter (cdr a) (+ 1 count))))
11 (length-iter items 0))
13 (define (append list1 list2)
14 (if (null? list1)
15 list2
16 (cons (car list1)
17 (append (cdr list1) list2))))
19 (define (last-pair l)
20 (cond ((null? l) (error "Empty List"))
21 ((null? (cdr l)) l)
22 (else (last-pair (cdr l)))))
24 (define (test-case actual expected)
25 (load-option 'format)
26 (newline)
27 (format #t "Actual: ~A Expected: ~A" actual expected))
29 (define (reverse l)
30 (if (null? l)
31 '()
32 (append
33 (reverse (cdr l))
34 (list (car l)))))
36 (define us-coins (list 50 25 10 5 1))
37 (define uk-coins (list 100 50 20 10 5 2 1 0.5))
39 (define (cc amount coin-values)
40 (cond ((= amount 0) 1)
41 ((or (< amount 0) (no-more? coin-values)) 0)
42 (else
43 (+ (cc amount
44 (except-first-denomination coin-values))
45 (cc (- amount
46 (first-denomination coin-values))
47 coin-values)))))
49 ;; 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?
51 (define first-denomination car)
52 (define except-first-denomination cdr)
53 (define no-more? null?)
55 ;; no, order of coin-values shouldn't matter because it doesn't matter in reali life
57 (test-case (cc 100 us-coins) 292)