SICP 习题解 第二章
生活随笔
收集整理的這篇文章主要介紹了
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)))))
總結
以上是生活随笔為你收集整理的SICP 习题解 第二章的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Fiddler抓包工具-拦截,断点
- 下一篇: c# 排列组合代码类