练习2.21-练习2.35

15 篇文章 0 订阅

1.练习2.21

(define (square-list items)
  (if (null? items)
      '()
      (cons (square (car items)) (square-list (cdr items)))))

(define (square-list items)
  (map (lambda (x) (square x)) items))

2.练习2.22

3.练习2.23

(define (for-each f items)
  (define (iter f items)
    (if (null? items)
	true
	(begin
	  (f (car items))
	  (iter f (cdr items)))))
  (iter f items))

4.练习2.24

5.练习2.25

(car (cdr (car (cdr (cdr (list 1 3 (list 5 7) 9))))))

(car (car (list (list 7))))

(car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7))))))))))))))))))

6.练习2.26

(1 2 3 4 5 6)
((1 2 3) 4 5 6)
((1 2 3) ( 4 5 6))

7.练习2.27

(define (deep-reverse items)
  (define (reverse-iter tmpitems result)
    (if (null? tmpitems)
	result
	(reverse-iter (cdr tmpitems)
		      (cons (if (number? (car tmpitems))
				(car tmpitems)
				(reverse-iter (car tmpitems) '()))
			    result))))
  (reverse-iter items '()))

8.练习2.28

(define (fringe trees)
  (cond ((null? trees) '())
	((not (pair? trees)) (list trees))
	(else (append (fringe (car trees)) (fringe (cdr trees))))))

9.练习2.29

(define (make-mobile left right)
  (list left right))

(define (make-branch length structure)
  (list length structure))

(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 (total-weigh _mobile)
  (if (not (pair? _mobile))
      _mobile
      (+ (total-weigh (branch-structure (left-branch _mobile)))
	 (total-weigh (branch-structure (right-branch _mobile))))))

(define (balance-weight _mobile)
  (define (iter _mobile result)
    (if (not (pair? _mobile))
      true
      (and result
	   (= (* (branch-length (left-branch _mobile))
		 (total-weigh (branch-structure (left-branch _mobile))))
	      (* (branch-length (right-branch _mobile))
		 (total-weigh (branch-structure (right-branch _mobile)))))
	   (balance-weight (branch-structure (left-branch _mobile)))
	   (balance-weight (branch-structure (right-branch _mobile))))))
  (iter _mobile true))

10.练习2.30

(define (square-tree tree)
  (cond ((null? tree) '())
	((not (pair? tree)) (square tree))
	(else (cons (square-tree (car tree))
		    (square-tree (cdr tree))))))

(define (square-tree tree)
  (map (lambda (sub-tree)
	 (if (pair? sub-tree)
	     (square-tree sub-tree)
	     (square sub-tree)))
       tree))

11.练习2.31

(define (tree-map f tree)
  (map (lambda (sub-tree)
	 (if (pair? sub-tree)
	     (tree-map f sub-tree)
	     (f sub-tree)))
       tree))

12.练习2.32

(define (subsets s)
  (if (null? s)
      (list '())
      (let ((rest (subsets (cdr s))))
	(append rest (map (lambda (x) (cons (car s) x)) rest)))))


13.练习2.33

(define (map p sequence)
  (accumulate (lambda (x y)
		(cons (p x) y))
	      '()
	      sequence))

(define (append seq1 seq2)
  (accumulate cons seq2 seq1))

(define (length sequence)
  (accumulate (lambda (x y) (+ 1 y)) 0 sequence))

14.练习2.34

(define (horner-eval x coefficient-sequence)
  (accumulate (lambda (this-coeff higher-terms) (+ this-coeff (* x higher-terms)))
	      0
	      coefficient-sequence))

15.练习2.35

(define (count-leaves tree)
    (accumulate +
                0
                (map (lambda (sub-tree)
                         (if (pair? sub-tree)        
                             (count-leaves sub-tree)   
                             1))                        
                     tree)))

(define (count-leaves t)
  (accumulate (lambda (x y) (if (not (pair? x))
				(+ x y)
				(+ (count-leaves x)
				   y)))
	      0
	      (map (lambda (x) (if (not (pair? x))
				   1
				   x))
		   (append (filter (lambda (x) (pair? x)) t)
			   (filter (lambda (x) (not (pair? x))) t)))))


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值