1 (define (map proc items)
4 (cons (proc (car items))
5 (map proc (cdr items)))))
7 (define (scale-list items factor)
8 (map (lambda (x) (* x factor))
11 ;; Exercise 2.21. The procedure square-list takes a list of numbers as argument and returns a list of the squares of those numbers.
13 (define (square-list-map nums)
14 (map (lambda (x) (* x x)) nums))
16 (define (square-list-recurse nums)
19 (cons (* (car nums) (car nums))
20 (square-list-recurse (cdr nums)))))
22 (define (test-case actual expected)
25 (format #t "Actual: ~A Expected: ~A" actual expected))
27 (test-case (square-list-recurse (list 1 2 3 4)) '(1 4 9 16))
28 (test-case (square-list-map (list 1 2 3 4)) '(1 4 9 16))
30 Exercise 2.22. Louis Reasoner tries to rewrite the first square-list procedure of exercise 2.21 so that it evolves an iterative process:
32 (define (square-list items)
33 (define (iter things answer)
37 (cons (square (car things))
41 Unfortunately, defining square-list this way produces the answer list in the reverse order of the one desired. Why?
43 Louis then tries to fix his bug by interchanging the arguments to cons:
45 (define (square-list items)
46 (define (iter things answer)
51 (square (car things))))))
54 This doesn't work either. Explain.