<< Previous exercise (3.15) | Index | Next exercise (3.17) >>
(count-pairs (list 'a 'b 'c)) ;; => 3 (define second (cons 'a 'b)) (define third (cons 'a 'b)) (define first (cons second third)) (set-car! third second) (count-pairs first) ;; => 4 (define third (cons 'a 'b)) (define second (cons third third)) (define first (cons second second)) (count-pairs first) ;; => 7 (define lst (list 'a 'b 'c)) (set-cdr! (cddr lst) lst) (count-pairs lst) ;; never returns
Another structure for the "return 4" case.
(define q '(a b)) (define r4 (cons q (cdr q))) (count-pairs r4) ; 4 ; r4 -> ( . ) ; | | ; | +-----+ ; v v ; ( . )-->( . )-> null ; | | ; 'a 'b
Anon