
SICP
_NMfloat_
但日有寸进,此心甚喜。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
SICP 习题2.53~2.55 equal
2.55 : ‘被替换成了(quote)(define (memq item x) (cond ((null? x) false) ((eq? item (car x)) x) (else (memq item (cdr x)))))(newline)(display (list 'a 'b 'c))(newline)(display (list (list原创 2016-07-01 19:09:18 · 516 阅读 · 0 评论 -
SICP 习题2.10 区间除法 被除区间横跨0的问题
横跨0会导致除法的结果是错的,因为x0\frac{x}{0}是没有结果的(define (add-interval x y) (make-interval (+ (lower-bound x) (lower-bound y)) (+ (upper-bound x) (upper-bound y))))(define (mul-interval x y) (let ((p1原创 2016-06-11 01:15:22 · 474 阅读 · 0 评论 -
SICP 习题2.9 区间宽度
可以看出add和sub之后的区间是之前两个区间的区间宽度之和,而mul和div是不确定的。(define (add-interval x y) (make-interval (+ (lower-bound x) (lower-bound y)) (+ (upper-bound x) (upper-bound y))))(define (mul-interval x y) (原创 2016-06-11 00:58:00 · 561 阅读 · 0 评论 -
SICP 习题2.7 实现区间的lower-bound 和 upper-bound
lower-bound 就是 carupper-bound 就是 cdrmake-interval 需要保证lower-bound 小于 upper-bound(define (add-interval x y) (make-interval (+ (lower-bound x) (lower-bound y)) (+ (upper-bound x) (upper-bound原创 2016-06-11 00:43:05 · 606 阅读 · 0 评论 -
SICP 习题2.8 区间减法
我的想法是区间减法是a区间的每一个数减去b区间的每一个数,这些相减的结果在一个区间上。也就是x的下界-y的上界是最小的,x的上界-y的下界是最大的。其他所有结果都在这两个数之间。(define (add-interval x y) (make-interval (+ (lower-bound x) (lower-bound y)) (+ (upper-bound x) (upp原创 2016-06-11 00:39:26 · 994 阅读 · 0 评论 -
SICP 习题2.5 2^a*3^b的cons car cdr过程
计算cos-b的结果就是2a3b2^a3^b的数值(car-b z)就是将z不断除2,就可以得到a,同理可以得到b因为2,3互质,所有整除的时候不会影响(define cons-b (lambda (x y) (cond ((and (> x 0) (> y 0)) (* 6 (cons-b (- x 1) (- y 1)))) ((> x 0) (* 2 (cons-b (原创 2016-06-10 22:46:51 · 572 阅读 · 0 评论 -
SICP 习题2.4 cons car cdr 用过程实现
(define (cons-a x y) (lambda (m) (m x y)))(define (car-a z) (z (lambda (p q) p)))(define (cdr-a z) (z (lambda (p q) q)))(define a (cons-a 1 2))(newline)(display (car-a a))(newline)(display (c原创 2016-06-10 19:28:18 · 636 阅读 · 0 评论 -
SICP 习题2.3 计算矩形面积和周长
我是用两个点表示的一个矩形,矩形的左下方的点和右上方的点。还有一种方法可以用一个左下方的点,一个长,一个宽就可以了。建立抽象屏障就是要求每个表示方法都能求出一个矩形的长和宽,根据这两个信息,就可以求出矩形面积和周长。(define make-rect cons)(define s-rect car)(define e-rect cdr)(define make-point cons)(defi原创 2016-06-10 19:17:15 · 787 阅读 · 0 评论 -
SICP 习题2.2 线段的中点
线段的中点(x1+x22,y1+y22)(\frac{x_1+x_2}{2},\frac{y_1+y_2}{2})(define make-segment cons)(define start-segment car)(define end-segment cdr)(define make-point cons)(define x-point car)(define y-point cdr)原创 2016-06-10 18:56:50 · 485 阅读 · 0 评论 -
SICP 习题2.1 make-rat 处理整数和负数版本
(define make-rat (lambda (n d) (let ((n1 (if (or (and (> n 0) (< d 0)) (and (< n 0) (> d 0))) -1 1)) (d1 1) (n2 (if (> n 0) n (* -1 n))) (d2 (if (> d 0) d (* -1 d)))) (原创 2016-06-10 17:53:30 · 490 阅读 · 0 评论 -
SICP 习题1.46 iterative-improve
LISP的括号非常重要,括号打错,整个程序就错了。(define (iterative-improve good-enough? improve) (define (iter x) ;(newline) ;(display x) ;(newline) ;(display "HELLO") (if (good-en原创 2016-06-10 12:59:15 · 365 阅读 · 0 评论 -
SICP 习题1.44 smooth
smooth函数(define (smooth f) (lambda (x) (/ (+ (f (- x dx)) (f x) (f (+ x dx))) 3)))(define dx 0.001)(define (compose f g) (lambda (x) (f (g x))))(define (repeated f times) (if (= times 1)原创 2016-06-05 00:10:25 · 424 阅读 · 0 评论 -
SICP 练习1.43 repeated
(define (compose f g) (lambda (x) (f (g x))))(define (repeated f times) (if (= times 1) f (compose f (repeated f (- times 1)))))(define (square x) (* x x))((repeated square 2) 5)原创 2016-06-04 21:50:55 · 463 阅读 · 0 评论 -
SICP 习题1. 42 compose
(define (compose f g) (lambda (x) (f (g x))))(define square (lambda (x) (* x x)))(define inc (lambda (x) (+ x 1)))((compose square inc) 6)原创 2016-06-04 18:13:50 · 346 阅读 · 0 评论 -
SICP 习题1.41 double
按照普通的想法来说,得出的结果应该是8+5,但是最后得到了16+5是这个样子的,(double double)相当于(four) (double(double double))相当于(four four)相当于把four应用4遍,于是就是16次了。(define (double f) (lambda (x) (f (f x))) )(define (inc n) (+ n 1))(new原创 2016-06-04 18:09:34 · 523 阅读 · 0 评论 -
SICP 习题1.40 cubic
cubic(define (cubic a b c) (lambda (x) (+ (* x x x) (* a x x) (* b x) c)))(define (cubic a b c) (lambda (x) (+ (* x x x) (* a x x) (* b x) c)))(define (deriv g) (lambda (x) (/ (- (g (+ x dx))原创 2016-06-04 17:10:17 · 386 阅读 · 0 评论 -
SICP 1.3.4 牛顿法
(define (deriv g) (lambda (x) (/ (- (g (+ x dx)) (g x)) dx)))(define dx 0.00001)(define (newton-transform g) (lambda (x) (- x (/ (g x) ((deriv g) x)))))(define (newtons-method g gues原创 2016-06-04 17:01:16 · 432 阅读 · 0 评论 -
SICP 练习1.39 计算tan(x)
(define (tan-cf x k) (define (d i) (- (* 2.0 i) 1.0)) (define (n i) (if (= i 1) x (* x x))) (define (iter i result) (if (= i 0) result (iter (- i 1) (/ (n i) (- (d i) result)原创 2016-06-04 16:15:27 · 387 阅读 · 0 评论 -
SICP 习题2.6 丘奇计数
计算cos-b的结果就是2a3b2^a3^b的数值(car-b z)就是将z不断除2,就可以得到a,同理可以得到b因为2,3互质,所有整除的时候不会影响(define cons-b (lambda (x y) (cond ((and (> x 0) (> y 0)) (* 6 (cons-b (- x 1) (- y 1)))) ((> x 0) (* 2 (cons-b (原创 2016-06-11 00:04:59 · 479 阅读 · 0 评论 -
SICP 习题2.11 改写div-interval 分情况讨论
x正正正负负负y正正正负负负\begin{array}{c|c}x&y\\正正&正正\\正负&正负\\负负&负负\end{array}3*3总共有9种情况,分类讨论即可。div-interval的代码(define (div-interval x y) (let ((x1 (lower-bound x)) (y1 (upper-bound x)) (x2 (/ 1 (lower-原创 2016-06-11 20:56:36 · 457 阅读 · 0 评论 -
SICP 习题2.43 八皇后问题flatmap嵌套反了
问题在于(queen-cols (- k 1))被执行过多次了,在之前2.42的里面(queen-cols (-k 1))只执行了一次棋盘大小是NN时,运行时间是T∗NNT*N^N,因为在每一层(queen-cols (- k 1))都要被执行N次,一共有N层。原创 2016-07-01 00:38:53 · 495 阅读 · 0 评论 -
SICP 习题2.42 八皇后问题
想不到第一次写八皇后问题是用的scm写代码的途中遇到一个错误,用let错了,用define对了,应该是编译器的原因,和nil差不多。(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr原创 2016-07-01 00:24:58 · 705 阅读 · 0 评论 -
SICP 习题2.61~2.62 排序表示的adjoin和union-set函数
adjoin函数要注意的是怎样把x插入当前的序列,union-set的写法可以参考书上给出的intersection-set。my.scm文件将给出一些所有代码经常会用到的逻辑。譬如这次的先换行后打印my.scm(define (out x) (newline) (display x))2.62.scm(load "my.scm")(define (element-of-set? x set原创 2016-07-22 19:14:02 · 602 阅读 · 0 评论 -
SICP 习题2.60 允许元素重复,重构集合操作
element-of-set? 和intersection-set的复杂度急剧上升,而adjoin-set和union-set的复杂度下降了一个O(n)O(n)。在adjoin-set操作和union-set操作比较多,element-of-set?和intersection-set操作比较少的时候(define (element-of-set? x set) (cond ((null? set原创 2016-07-22 15:14:17 · 387 阅读 · 0 评论 -
SICP 习题2.59 union-set操作
union-set操作的想法就是找出set1中set2里面没有的,set2中没有的和set2放在一起,就是union-set(define (element-of-set? x set) (cond ((null? set) false) ((equal? x (car set)) true) (else (element-of-set? x (cdr set))原创 2016-07-22 14:49:17 · 403 阅读 · 0 评论 -
SICP 习题2.41 triple 三元组
非常朴素的想法,找出所有的三元组,然后判定三元组的和是否与s相等(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))))(define (enumerate-in原创 2016-06-29 21:38:55 · 588 阅读 · 0 评论 -
SICP 练习2.40 unique-pairs
我所做的只是把unique-pairs封装了一下(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))))(define (enumerate-interval l原创 2016-06-29 19:47:21 · 501 阅读 · 0 评论 -
SICP 联系2.28 实现fringe
(define (fringe x) (cond ((null? (car x)) #f) ((pair? (car x)) (fringe (car x))) (else (out-elem (car x)))) (cond ((null? (cdr x)) #f) ((pair? (cdr x)) (fringe (cdr x))) (else (out-原创 2016-06-12 22:34:02 · 445 阅读 · 0 评论 -
SICP 联系2.27 实现deep-reverse
(define x (list (list 1 2) (list 3 4)))(define (deep-reverse s) (cond ((not (pair? s)) s) (else (cons (deep-reverse (cdr s)) (deep-reverse (car s))))))(deep-reverse x)原创 2016-06-12 22:25:02 · 396 阅读 · 0 评论 -
SICP 习题2.23 实现for-each
我还是用了前面那个非常讨巧的办法,两个if(define (for-each proc items) (if (not (null? items)) (proc (car items))) (if (not (null? items)) (for-each proc (cdr items))))(for-each (lambda (x) (newline) (display x)) '(57原创 2016-06-12 22:00:05 · 396 阅读 · 0 评论 -
SICP 习题2.22 square迭代式写法的失败
代码产生的结果是((((()1)2)3)4)((((()1)2)3)4)而我们需要的是(1(2(3(4))))(1(2(3(4))))(define (square-list items) (define (iter things answer) (if (null? things) answer (iter (cdr things) (cons原创 2016-06-12 21:54:10 · 402 阅读 · 0 评论 -
SICP 习题2.21 square-list
print-list的时候写了三个if,是个愚蠢的地方(define (map-a proc items) (if (null? items) '() (cons (proc (car items)) (map-a proc (cdr items)))))(define nl newline)(define print-list (lambda (s) (原创 2016-06-12 21:29:49 · 373 阅读 · 0 评论 -
SICP 习题2.20 same-parity
(define same-parity (lambda (x . y) (define iter (lambda (z res) (if (null? z) res (if (= (remainder (car z) 2) (remainder x 2)) (iter (cdr z) (cons res (car z)))原创 2016-06-12 20:06:56 · 512 阅读 · 0 评论 -
SICP 习题2.19 重写count-change 过程
(define (cc amount coin-values) (cond ((= amount 0) 1) ((or (< amount 0) (no-more? coin-values)) 0) (else (+ (cc amount (except-first-denomination coin-values)) (cc (原创 2016-06-12 18:47:11 · 1164 阅读 · 0 评论 -
SICP 习题2.17 last-pair 找出表的最后一个值
比较简单,判断(cdr s)是否为空即可(define last-pair (lambda (s) (if (null? (cdr s)) (car s) (last-pair (cdr s)))))(last-pair '(23 72 149 34))原创 2016-06-12 18:09:27 · 426 阅读 · 0 评论 -
SICP 练习2.13 乘积区间的误差
被乘区间(x1,y1)和(x2,y2)(x_1,y_1)和(x_2,y_2)题目中只考虑正数,所以乘积是(x1x2,y1y2)(x_1x_2,y_1y_2)一个区间的误差为y1−x12y1+x12=y1−x1y1+x1\frac{\frac{y_1-x_1}{2}}{\frac{y_1+x_1}{2}}=\frac{y_1-x_1}{y_1+x_1}y1−x1y1+x1+y2−x2y2+x2\fra原创 2016-06-11 21:36:56 · 590 阅读 · 0 评论 -
SICP 练习2.12 make-interval-percent
(define make-interval cons)(define lower-bound car)(define upper-bound cdr)(define print-interval (lambda (z) (newline) (display (lower-bound z)) (display " ~ ") (display (upper-bou原创 2016-06-11 21:11:22 · 479 阅读 · 0 评论 -
SICP 练习1.38 寻找e的值
(define (cont-frac-linear n d k) (define (iter i result) (if (= i 0) result (iter (- i 1) (/ (n i) (+ (d i) result))))) (iter k 0))(define (n i) (cond ((= (remainder i 3) 2) (* (/原创 2016-06-04 15:55:52 · 366 阅读 · 0 评论 -
SICP 练习1.37 计算黄金分割律
cont−frac是递归cont-frac是递归cont−frac−linear是迭代cont-frac-linear是迭代(define (cont-frac n d k) (define (iter i) (if (= i k) (/ (n i) (d i)) (/ (n i) (+ (d i) (iter (+ i 1)))))) (原创 2016-06-04 15:39:56 · 571 阅读 · 0 评论 -
SICP 习题1.36 使用不动点发寻找x^x = 1000的根
修改fixed-point过程,在let里修改即可(define tolerance 0.00001)(define (fixed-point f first-guess) (newline) (display first-guess) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (原创 2016-06-04 15:04:44 · 758 阅读 · 0 评论