SICP 习题解 第二章

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

Structure and Interpretation os Computer Programs Exercises Answer

第二章 构造数据抽象

练习2.17

    (define last-pair-1
      (lambda (input)
        (if (= (length input) 1)
            input
            (last-pair-1 (cdr input)))))
	;因为length的定义是递归定义的,所以如果是一个长列表,用length会非常耗时
	;last-pair-2和last-pair-3用null?去检测列表是否为空,效率会更高
    (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

    (define reverse-1
      (lambda (input)
        (if (null? input)
            '()
            (append (reverse-1 (cdr input)) (cons (car input) '())))))
    ;用迭代的方式效率更高       
    (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的排列顺序给结果没有关系,是否有序排列结果都是一样的。

    (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

    (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

    (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

    (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

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

    (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

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

    (define (left-branch bran)
      (car bran))
    (define (right-branch)
      (car (cdr bran)))

练习2.30

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

    (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

    (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

    (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

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

练习2.35

练习2.36

    (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)))))
posted @ 2014-03-09 12:21  Liqiang Gao  阅读(287)  评论(0编辑  收藏  举报