SCIP学习笔记-ex2.25--ex2.31

6 篇文章 0 订阅
6 篇文章 0 订阅
;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))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值