-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.22.rkt
27 lines (23 loc) · 847 Bytes
/
2.22.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#lang sicp
;; Exercise 2.22: Louis Reasoner tries to rewrite the first square-list procedure of Exercise 2.21 so that it evolves an iterative process:
(define (square-list items)
(define (iter things answer)
(if (null? things)
answer
(iter (cdr things)
(cons (square (car things))
answer))))
(iter items nil))
;; Unfortunately, defining square-list this way produces the answer list in the reverse order of the one desired. Why?
;;
;; Louis then tries to fix his bug by interchanging the arguments to cons:
(define (square-list items)
(define (iter things answer)
(if (null? things)
answer
(iter (cdr things)
(cons answer
(square
(car things))))))
(iter items nil))
;; This doesn’t work either. Explain.