首页 > 其他 > 详细

SICP 习题解 第二章

时间:2014-03-11 12:17:55      阅读:478      评论:0      收藏:0      [点我收藏+]

计算机程序的构造和解释习题解答

Structure and Interpretation os Computer Programs Exercises Answer

第二章 构造数据抽象

练习2.17

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(define last-pair-1
  (lambda (input)
    (if (= (length input) 1)
        input
        (last-pair-1 (cdr input)))))
;因为length的定义是递归定义的,所以如果是一个长列表,用length会非常耗时
;last-pair-2和last-pair-3null?去检测列表是否为空,效率会更高
(define last-pair-2
  (lambda (input)
    (if (null? (cdr input))
        input
        (last-pair-2 (cdr input)))))
(define last-pair-3
  (lambda (input)
    (let ([last (cdr input)])
      (if (null? last)
          input
          (last-pair-3 last)))))

练习2.18

1
2
3
4
5
6
7
8
9
10
11
12
13
14
(define reverse-1
  (lambda (input)
    (if (null? input)
        ‘()
        (append (reverse-1 (cdr input)) (cons (car input) ‘())))))
;用迭代的方式效率更高<br>
(define reverse-2
  (lambda (input)
    (define reverse-iter
      (lambda (remain record)
        (if (null? remain)
          record
          (reverse-iter (cdr remain) (cons (car remain) record)))))
  (reverse-iter input ‘())))

练习2.19

理解换零钱的逻辑,将总数为a的现金换成n种硬币的不同方式等于:

  • 不考虑第一种硬币,将现金a换成除第一种硬币之外的所有其他硬币的不同方式数目,加上
  • 考虑第一种硬币,将现金a-v[0]换成所有种类的硬币的不同方式的数目。

所以表coin-values的排列顺序给结果没有关系,是否有序排列结果都是一样的。

1
2
3
4
5
6
7
8
9
10
11
(define first-denomination
  (lambda (coin-values)
    (car coin-values)))
(define except-first-denomination
  (lambda (coin-values)
    (cdr coin-values)))
(define no-more?
  (lambda (coin-values)
    (if (null? coin-values)
        #t
        #f)))

练习2.21

1
2
3
4
5
6
(define (square-list-1 items)
  (if (null? items)
      ‘()
      (cons (* (car items) (car items)) (square-list-1 (cdr items)))))
(define (square-list-2 items)
  (map (lambda (x) (* x x)) items))

练习2.22

(cons x y)的作用是把x当成一个元素插入到列表y的开头,如果x本身是一 个列表,x会以列表的身份插入到y开头。 比如(cons ‘(1) ‘(2 3))的结果不是‘(1 2 3),而是‘(‘(1) 2 3)。 此处可以使用append。

练习2.23

1
2
3
4
5
6
7
8
9
(define (for-each-1 func items)
  (if (null? items)
      #f
      (or (func (car items))
          (for-each-1 func (cdr items)))))    (define (for-each-1 func items)
  (if (null? items)
      #f
      (or (func (car items))
          (for-each-1 func (cdr items)))))

练习2.24

练习2.25

1
2
3
(car (cdr (car (cdr (cdr ‘(1 3 (5 7 9)))))))
(car (car ‘((7))))
(car (cdr (car (cdr (car (cdr (car(cdr (car (cdr (car (cdr ‘(1 (2 (3 (4 (5 (6 7))))))))))))))))))

练习2.26

1
2
3
- (append x y): ‘(1 2 3 4 5 6)
- (cons x y): ‘((1 2 3) (4 5 6))
- (list x y): ‘((1 2 3) (4 5 6))

练习2.27

1
2
3
4
5
(define (deep-reverse items)
  (cond
    [(null? items) (list)]
    [(not (pair? (car items))) (append (deep-reverse (cdr items)) (list (car items)))]
    [else (append (deep-reverse (cdr items)) (cons (deep-reverse (car items)) (list)))]))

练习2.28

1
2
3
4
5
6
(define (fringe items)
  (if (null? items)
      ‘()
      (if (pair? (car items))
          (append (fringe (car items)) (fringe (cdr items)))
          (cons (car items) (fringe (cdr items))))))

练习2.29

a)

1
2
3
4
(define (left-branch bran)
  (car bran))
(define (right-branch)
  (car (cdr bran)))

练习2.30

1)不使用高阶函数,直接定义

1
2
3
4
5
6
(define (square-tree items)
  (if (null? items)
      (list)
      (if (not (pair? (car items)))
          (cons (* (car items) (car items)) (square-tree (cdr items)))
          (append (cons (square-tree (car items)) (list)) (square-tree (cdr items))))))

2)使用map

1
2
3
4
5
6
(define (tree-map proc trees)
  (if (null? trees)
      (list)
      (if (not (pair? (car trees)))
          (cons (proc (car trees)) (tree-map proc (cdr trees)))
          (append (cons (tree-map proc (car trees)) (list)) (tree-map proc (cdr trees))))))

练习2.31

答案同2.30

练习2.32

练习2.33

1
2
3
4
5
6
(define (map p sequence)
  (accumulate (lambda (x y) (p x)) ‘() sequence))
(define (append seq1 seq2)
  (accumulate cons seq2 seq1))
(define (length sequence)
  (accumulate (lambda (x y) (+ y 1)) 0 sequence))

练习2.34

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

练习2.35

练习2.36

1
2
3
4
5
6
7
8
9
10
11
12
13
(define (first-elems items)
  (if (null? items)
      (list)
      (cons (car (car items)) (first-elems (cdr items)))))
(define (rest-elems items)
  (if (null? items)
      (list)
      (cons (cdr (car items)) (rest-elems (cdr items)))))
(define (accumulate-n op init seqs)
  (if (null? (car seqs))
      (list)
      (cons (accumulate op init (first-elems seqs))
            (accumulate-n op init (rest-elems seqs)))))

SICP 习题解 第二章,布布扣,bubuko.com

SICP 习题解 第二章

原文:http://www.cnblogs.com/richard-g/p/3589813.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!