Hatena::Grouphokudaisicp

TSBの日記

 | 

2008-12-08

2.2.2

| 17:54


;;2.2.2

(define (count-leaves x)
  (cond ((null? x) 0)
	 ((not (pair? x)) 1)
	 (else (+ (count-leaves (car x))
	 	   (count-leaves (cdr x))))))

(define x (cons (list 1 2) (list 3 4)))
(print (length x))
(print x)

(define x2 (cons (list 1 2) (list (list 3 4) 5)))
(print (length x2))
(print x2)

(define x3 (cons (list 10 11) x))
(print (length x3))
(print x3)

(define x4 (cons x (list 11 12)))
(print (length x4))
(print x4)
(print (cdr x4))

(define x5 (cons x x))
(print (length x5))
(print x5)

(define x6 (cons x (list x x)))
(print (length x6))
(print x6)
(print (count-leaves x6))

;;2.24
(define tree1 (cons 1 (list 2 (list 3 4))))
(print tree1)
(print (length tree1))
(print (count-leaves tree1))

;;2.25
(define 7tree1 (list 1 3 (list 5 7) 9))
(print 7tree1)
(print (cdr (car (cdr (cdr 7tree1)))))

 |