SICP Practice

Practice 3.16

(define (count-pairs x)
  (if (not (pair? x))
      0
      (+ (count-pairs (car x))
         (count-pairs (cdr x))
         1)))

z1

(define z1 (cons (list 'a) (list 'b)))
z1                                      ; ((a) b)
(count-pairs z1)                        ; 3

z2

(define a (list 'a))
(define z2 (cons a (cons a '())))
z2                                      ; ((a) (a))
(count-pairs z2)                        ; 4

z3

(define b (list 'b))
(define c (cons b b))
(define z3 (cons c c))
z3                                      ; (((b) b) (b) b)
(count-pairs z3)                        ; 7

z3

(define d (list 'd))
(define e (cons d '()))
(define z4 (cons d e))
(set-car! d e)
z4                                      
(count-pairs z4)                        ; not return value