<< 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)))
jz