;Exercise 2.25. Give combinations of cars and cdrs that will pick 7
;from each of the following lists:
;(1 3 (5 7) 9)
(define ex2.25-list1 (list 1 3 (list 5 7) 9))
(define ex2.25-list1-get-7 (car (cdr (car ( cdr (cdr ex2.25-list1))))))
;((7))
(define ex2.25-list2 (list (list 7)))
(define ex2.25-list2-get-7 (car (car ex2.25-list2)))
;(1 (2 (3 (4 (5 (6 7))))))
(define ex2.25-list3 (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7)))))))
(define (cad x) (car (cdr x)))
(define ex2.25-list3-get-7 (cad (cad (cad (cad (cad (cad ex2.25-list3)))))))
;Exercise 2.27. Modify your reverse procedure of exercise 2.18 to produce a deep-reverse
;procedure that takes a list as argument and returns as its value the list with its elements
;reversed and with all sublists deep-reversed as well.
(define (deep-reverse l)
(cond ((not (pair? l)) l)
(else (list (deep-reverse (car (cdr l))) (deep-reverse (car l))))))
;(define x (list (list 1 2) (list 3 4)))
;x
;(deep-reverse x)
;Exercise 2.28. Write a procedure fringe that takes as argument a tree (represented as a list)
;and
;returns a list whose elements are all the leaves of the tree arranged in left-to-right order.
;(define x (list (list 1 2) (list 3 4)))
;x
;(define y (list (list (list 1))))
;y
(define (fringe l)
(cond ((pair? l)(cond ((not (null? (cdr l)))
(append (fringe (car l)) (fringe (car (cdr l)))))
((not (null? (car l)))
(fringe (car l)))))
(else (list l))))
;(fringe x)
;(fringe y)
;ex2.29
(define (make-mobile left right)
(list left right))
(define (make-branch length structure)
(list length structure))
;a.
(define (left-branch mobile)
(car mobile))
(define (right-branch mobile)
(car (cdr mobile)))
(define (branch-length branch)
(car branch))
(define (branch-structure branch)
(car (cdr branch)))
(define branch-a (make-branch 10 4))
(define branch-b (make-branch 5 8))
(define branch-c (make-branch 20 2))
(define branch-d (make-branch 4 10))
(define mobile-a (make-mobile branch-a branch-b))
(define mobile-b (make-mobile branch-c branch-d))
(define branch-e (make-branch 6 mobile-a))
(define branch-f (make-branch 6 mobile-b))
(define mobile-c (make-mobile branch-e branch-f))
;mobile-c
;b.
(define (branch-weight branch)
(if (not (pair? (branch-structure branch)))
(branch-structure branch)
(total-weight (branch-structure branch))))
(define (total-weight mobile)
(+ (branch-weight (left-branch mobile))
(branch-weight (right-branch mobile))))
;(total-weight mobile-c)
;c.
(define (balanced mobile)
(define (torque branch)
(* (branch-length branch)
(branch-weight branch)))
(define (branch-balanced branch)
(if (not (pair? (branch-structure branch)))
#t
(balanced (branch-structure branch))))
(cond ((not (branch-balanced (left-branch mobile)))
#f)
((not (branch-balanced (right-branch mobile)))
#f)
(else (= (torque (left-branch mobile)) (torque (right-branch mobile))))))
;(balanced mobile-c)
;ex2.30
(define (scale-tree tree factor)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(scale-tree sub-tree factor)
(* sub-tree factor)))
tree))
(define ex-2.30-scale-tree (list 1 (list 2 (list 3 4) 5) (list 6 7)) )
;(scale-tree ex-2.30-scale-tree 10)
(define (square-tree tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(square-tree sub-tree)
(* sub-tree sub-tree)))
tree))
(square-tree ex-2.30-scale-tree)
; ex2.31 -----------
(define (tree-map proc tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(tree-map proc sub-tree)
(proc sub-tree )))
tree))
(tree-map square ex-2.30-scale-tree)
(define (subsets s)
(if (null? s)
(list null)
(let ((rest (subsets (cdr s))))
(append rest (map (lambda (sub-s) (append (list (car s)) sub-s )) rest)))))
(subsets (list 1 2 3 4))
SCIP学习笔记-ex2.25--ex2.31
最新推荐文章于 2021-03-09 15:38:35 发布