You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
828 B
33 lines
828 B
2 years ago
|
|
||
|
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.
|