练习2.21-练习2.35

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)))))


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页