<< Previous exercise (2.21) | Index | Next exercise (2.23) >>
Saying in a different way, the bug on the second version can also be attributed to the property of cons as shown below.
1 ]=> (cons 1 2) ;Value 11: (1 . 2) 1 ]=> (cons 1 (list 2 3)) ;Value 12: (1 2 3) 1 ]=> (cons (list 2 3) 1) ;Value 13: ((2 3) . 1)
An implementation evolving an iterative process works.
(define (square-list items)
(define (iter l pick)
(define r (square (car l)))
(if (null? (cdr l))
(pick (list r))
(iter (cdr l) (lambda (x) (pick (cons r x))))))
(iter items (lambda (x) x)))
the problem here is that Louis uses iteration and now we can only use cdr to get the rest of the list, so we must process the list from the tail by iteration, and since the answer is originally nil, we can't just change the order to (cons answer (square (car things))) because when the program executed to the innermost iteration, the first element will be nil, so the result will be like (((() . 1) . 4) . 9)
jz