日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

SICP 习题解 第二章

發(fā)布時(shí)間:2024/9/5 编程问答 57 如意码农
生活随笔 收集整理的這篇文章主要介紹了 SICP 习题解 第二章 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

計(jì)算機(jī)程序的構(gòu)造和解釋習(xí)題解答

Structure and Interpretation os Computer Programs Exercises Answer

第二章 構(gòu)造數(shù)據(jù)抽象

練習(xí)2.17

    (define last-pair-1
(lambda (input)
(if (= (length input) 1)
input
(last-pair-1 (cdr input)))))
;因?yàn)閘ength的定義是遞歸定義的,所以如果是一個(gè)長(zhǎng)列表,用length會(huì)非常耗時(shí)
;last-pair-2和last-pair-3用null?去檢測(cè)列表是否為空,效率會(huì)更高
(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)))))

練習(xí)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 '())))

練習(xí)2.19

理解換零錢的邏輯,將總數(shù)為a的現(xiàn)金換成n種硬幣的不同方式等于:

  • 不考慮第一種硬幣,將現(xiàn)金a換成除第一種硬幣之外的所有其他硬幣的不同方式數(shù)目,加上
  • 考慮第一種硬幣,將現(xiàn)金a-v[0]換成所有種類的硬幣的不同方式的數(shù)目。

所以表coin-values的排列順序給結(jié)果沒有關(guān)系,是否有序排列結(jié)果都是一樣的。

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

練習(xí)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))

練習(xí)2.22

(cons x y)的作用是把x當(dāng)成一個(gè)元素插入到列表y的開頭,如果x本身是一

個(gè)列表,x會(huì)以列表的身份插入到y(tǒng)開頭。

比如(cons '(1) '(2 3))的結(jié)果不是'(1 2 3),而是'('(1) 2 3)。

此處可以使用append。

練習(xí)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)))))

練習(xí)2.24

練習(xí)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))))))))))))))))))

練習(xí)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))

練習(xí)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)))]))

練習(xí)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))))))

練習(xí)2.29

a)

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

練習(xí)2.30

1)不使用高階函數(shù),直接定義

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

練習(xí)2.31

答案同2.30

練習(xí)2.32

練習(xí)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))

練習(xí)2.34

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

練習(xí)2.35

練習(xí)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)))))

總結(jié)

以上是生活随笔為你收集整理的SICP 习题解 第二章的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。