練習2.24 2.26

#|
(define x (cons (list 1 2) (list 3 4)))
(length x)

(define (count-leaves tree)
  (cond ((null? tree) 0)
        ((not (pair? tree)) 1) ;: 葉子
        (else (+ (count-leaves (car tree))
                 (count-leaves (cdr tree))))))

(count-leaves x)

|#
;: 練習 2.24
(list 1 (list 2 (list 3 4))) ;: '(1 (2 ( 3 4)))

#|
;: 盒子

(list a b) => (cons a (cons b nil))
;: 圖爲
[.][.]-->[.][.]-->nil
 |        |
 |        |
 V        V
 a        b



(list 1 (list 2 (list 3 4))) ;:  
[.][.]--------------->[.][.]-->nil
|                      |
|                      |
V                      V        
1  (list 2 (list 3 4))[.][.]--->[.][.]-->nil 
                       |         |
                       |         |
                       V         |
                       2         V
                      (list 3 4)[.][.]-->[.][.]-->nil
                                 |        |           
                                 |        |              
                                 V        V                 
                                 3        4                 


;: 樹

(1 (2 (3 4)))
    .
   / \
  1   . (2 (3 4))
     / \
    2   .( 3 4)
       / \
      3  4
|#



;:  練習 2.25
(car ;:7
 (cdr ;: '(7)
  (car ;: '(5 7)
   (cdr ;: '((5 7) 9) 
    (cdr '(1 2 (5 7) 9)) ;: '(2 (5 7) 9)
    ))))

(car (car '((7))))


(car ;: 7
 (cdr ;: '(7)
  (cdr
   (cdr
    (cdr
     (cdr
      (cdr '(1 2 3 4 5 6 7) ;:'(2 3 4 5 6 7)
           )))))))


#|
;: 練習 2.6
(define x (list 1 2 3))
(define y (list 4 5 6))
(cons x y) ;: '((1 2 3) 4 5 6)
(list x y) ;: '((1 2 3) (4 5 6))
|#
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章