Blame


1 665c255d 2023-08-04 jrmu (define (map proc items)
2 665c255d 2023-08-04 jrmu (if (null? items)
3 665c255d 2023-08-04 jrmu '()
4 665c255d 2023-08-04 jrmu (cons (proc (car items))
5 665c255d 2023-08-04 jrmu (map proc (cdr items)))))
6 665c255d 2023-08-04 jrmu
7 665c255d 2023-08-04 jrmu (define (scale-list items factor)
8 665c255d 2023-08-04 jrmu (map (lambda (x) (* x factor))
9 665c255d 2023-08-04 jrmu items))
10 665c255d 2023-08-04 jrmu
11 665c255d 2023-08-04 jrmu (define (square-list-map nums)
12 665c255d 2023-08-04 jrmu (map (lambda (x) (* x x)) nums))
13 665c255d 2023-08-04 jrmu
14 665c255d 2023-08-04 jrmu (define (square-list-recurse nums)
15 665c255d 2023-08-04 jrmu (if (null? nums)
16 665c255d 2023-08-04 jrmu '()
17 665c255d 2023-08-04 jrmu (cons (* (car nums) (car nums))
18 665c255d 2023-08-04 jrmu (square-list-recurse (cdr nums)))))
19 665c255d 2023-08-04 jrmu
20 665c255d 2023-08-04 jrmu ;; Exercise 2.22. Louis Reasoner tries to rewrite the first square-list procedure of exercise 2.21 so that it evolves an iterative process:
21 665c255d 2023-08-04 jrmu
22 665c255d 2023-08-04 jrmu (define (square-list items)
23 665c255d 2023-08-04 jrmu (define (iter things answer)
24 665c255d 2023-08-04 jrmu (if (null? things)
25 665c255d 2023-08-04 jrmu answer
26 665c255d 2023-08-04 jrmu (iter (cdr things)
27 665c255d 2023-08-04 jrmu (cons (square (car things))
28 665c255d 2023-08-04 jrmu answer))))
29 665c255d 2023-08-04 jrmu (iter items nil))
30 665c255d 2023-08-04 jrmu
31 665c255d 2023-08-04 jrmu ;; (cons (square (car things))
32 665c255d 2023-08-04 jrmu ;; answer)
33 665c255d 2023-08-04 jrmu ;; puts the next number in front of its previous number, but we should be
34 665c255d 2023-08-04 jrmu ;; putting the next number behind the previous number
35 665c255d 2023-08-04 jrmu ;; that's why the numbers appear reversed in the resulting list
36 665c255d 2023-08-04 jrmu
37 665c255d 2023-08-04 jrmu ;; Louis then tries to fix his bug by interchanging the arguments to cons:
38 665c255d 2023-08-04 jrmu
39 665c255d 2023-08-04 jrmu (define (square-list items)
40 665c255d 2023-08-04 jrmu (define (iter things answer)
41 665c255d 2023-08-04 jrmu (if (null? things)
42 665c255d 2023-08-04 jrmu answer
43 665c255d 2023-08-04 jrmu (iter (cdr things)
44 665c255d 2023-08-04 jrmu (cons answer
45 665c255d 2023-08-04 jrmu (square (car things))))))
46 665c255d 2023-08-04 jrmu (iter items nil))
47 665c255d 2023-08-04 jrmu
48 665c255d 2023-08-04 jrmu ;; This doesn't work either. Explain.
49 665c255d 2023-08-04 jrmu
50 665c255d 2023-08-04 jrmu ;; answer is a list whereas (square (car things)) is a number. So although
51 665c255d 2023-08-04 jrmu ;; the order is right, you end up with nested lists. We should instead be
52 665c255d 2023-08-04 jrmu ;; using (append answer (list (square (car things))))
53 665c255d 2023-08-04 jrmu ;; to append two lists
54 665c255d 2023-08-04 jrmu
55 665c255d 2023-08-04 jrmu (define (test-case actual expected)
56 665c255d 2023-08-04 jrmu (load-option 'format)
57 665c255d 2023-08-04 jrmu (newline)
58 665c255d 2023-08-04 jrmu (format #t "Actual: ~A Expected: ~A" actual expected))
59 665c255d 2023-08-04 jrmu