Structure and Interpretation os Computer Programs Exercises Answer
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- 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))))) |
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 ‘()))) |
理解换零钱的逻辑,将总数为a的现金换成n种硬币的不同方式等于:
所以表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))) |
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)) |
(cons x y)的作用是把x当成一个元素插入到列表y的开头,如果x本身是一 个列表,x会以列表的身份插入到y开头。 比如(cons ‘(1) ‘(2 3))的结果不是‘(1 2 3),而是‘(‘(1) 2 3)。 此处可以使用append。
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))))) |
略
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 )))))))))))))))))) |
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 )) |
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)))])) |
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)))))) |
a)
1
2
3
4 |
(define (left-branch bran) (car bran)) (define (right-branch) (car (cdr bran))) |
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.30
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)) |
1
2
3
4 |
(define (horner-eval x coefficient-sequence) (accumulate (lambda (this-coeff higher-terms) (+ this-coeff (* x higher-terms))) 0 coefficient-sequence)) |
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))))) |
原文:http://www.cnblogs.com/richard-g/p/3589813.html