(define (filter predicate sequence)
(cond ((null? sequence) nill)
((predicate (car sequence))
(cons (car sequence)
(filter predicate (cdr sequence))))
(else (filter predicate (cdr sequence)))))
(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
(define (enumerate-interval low high)
(if (> low high)
nil
(cons low (enumerate-interval (+ low 1) high))))
;2.33
(define (map p sequence)
(accumulate (lambda (x y) (cons (p x) y)) nil sequence))
;(map (lambda (x) (* x x)) (list 1 2 3 4))
(define (append seq1 seq2)
(accumulate cons seq2 seq1))
;(append (list 1 2 3) (list 3 4 5))
(define (length sequence)
(accumulate (lambda (x y) (+ 1 y)) 0 sequence))
;(length (list 1 2 3))
;2.34
(define (horner-eval x coefficient-sequence)
(accumulate (lambda (this-coeff higher-terms)
(+ this-coeff (* x higher-terms)))
0
coefficient-sequence))
;(horner-eval 2 (list 1 3 0 5 0 1))
;2.35
(define (count-leaves t)
(accumulate +
0
(map (lambda (x)
(if (pair? x) (count-leaves x)
1))
t)))
;(define x (cons (list 1 2) (list 3 4)))
;(count-leaves (list x x))
;2.36
(define (accumulate-n op init seqs)
(if (null? (car seqs))
nil
(cons (accumulate op init (map car seqs))
(accumulate-n op init (map cdr seqs)))))
(define s (list (list 1 2 3) (list 4 5 6) (list 7 8 9) (list 10 11 12)))
;(accumulate-n + 0 s)
;; 2.38
(define (fold-left op initial sequence)
(define (iter result rest)
(if (null? rest)
result
(iter (op result (car rest))
(cdr rest))))
(iter initial sequence))
(define (fold-right op initial sequence)
(accumulate op initial sequence))
;(fold-right / 1 (list 1 2 3))
;(fold-left / 1 (list 1 2 3))
;(fold-right list nil (list 1 2 3))
;(fold-left list nil (list 1 2 3))
;;2.39
(define (reverse sequence)
(fold-right (lambda (x y)
(append y
(cons (if (pair? x) (reverse x)
x)
nil)))
nil
sequence))
(define (reverse sequence)
(fold-left (lambda (x y)
(append (cons (if (pair? y) (reverse y)
y)
nil)
x))
nil
sequence))
;(reverse (list 1 2 3 4 5 6))
2.37中的线性代数都已经忘光了,略过。
2.38的答案是,op要符合 a op b = b op a 的原则,如+,*。
分享到:
相关推荐
SICP 习题答案 计算机程序的构造和解释 1-3章 习题答案
NULL 博文链接:https://pengpeng.iteye.com/blog/1344689
SICP习题解答,主要第一章的内容习题答案
sicp in python 中文版 sicp in python 中文版 sicp in python 中文版 !!!download>>>https://github.com/wizardforcel/sicp-py-zh
SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版
SICP-Python版本
SICP 使用的scheme解释器 以前叫DrScheme
sicp 2.2.4节图形语言的racket程序包,配置路径,C:\Users\Administrator\AppData\Roaming\Racket
Python SICP epub版本,很适合学习抽象的思想,用Python版本比lisp更实用
sicp 2ed高清pdf,以及相对应的mit课程资料及习题答案打包,中文版的视频在这里http://i.youku.com/i/UNTcxODk3ODQw/videos?spm=a2hzp.8244740.0.0
SICP 解题集
SICP CHINESE ENGLISH THE SECOND EDITION SICP CHINESE ENGLISH THE SECOND EDITION
sicp in python 中文版 sicp in python 中文版 sicp in python 中文版 download : https://github.com/wizardforcel/sicp-py-zh
SICP_章_1-3 我对SICP第二版第1-3章的解决方案: 书中练习来自示例分配/ psets 项目位于 最后,来自考试
资源来自pypi官网。 资源全名:sicp-0.0.1b102.dev4.tar.gz
sicp-in-python(中文版+英文版)PDF 背景. SICP 全称Structure and Interpretation of Computer Programs,翻译过来叫《计算机程序的构造和解释》使用python
SICP 解题集《计算机程序的构造和解释》练习题解集。安装 MIT/GNU Scheme(macOS)下载:下载后运行 .dmg 文件,把 MIT/GNU Scheme.app 拖入 Applications 文件夹在 Applications/应用程序 文件夹中找到 MIT/GNU ...
资源名称:sicp 和 操作系统:精髓与设计原理第七版资源截图: 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。
经典书籍《计算机程序的构造与解释》,UCB热门课程CS61a的官方教材