SICP Practice

Practice 3.17

	(define (count-pairs x)
	  (let ((counted-list '()))

	    (define (counted? x)
	      (define (counted-temp? x counted-list)
	        (cond ((null? counted-list) false)
	              ((eq? (car counted-list) x) true)
	              (else (counted-temp? x (cdr counted-list)))))
	      (counted-temp? x counted-list))

	    (define (add-to-counted-list x)
	      (set! counted-list (cons x counted-list)))
	    
	    (define (count-pairs-temp x)
	      (cond ((not (pair? x)) 0)
	            ((counted? x) 0)
	            (else
	             (begin (display counted-list)
	                    (newline)
	                    (add-to-counted-list x)
	                    (display counted-list)
	                    (newline)
	                    (+ (count-pairs-temp (car x))
	                       (count-pairs-temp (cdr x))
	                       1)))))
	    (count-pairs-temp x)))

	(define a (cons 2 '()))
	(define t1 (cons a (cons a '())))

	(define b (cons 3 4))
	(define c (cons b b))
	(define d (cons c c))
	(define t2 (cons d d))

	(count-pairs t1)
	(count-pairs t2)