自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(69)
  • 收藏
  • 关注

原创 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 531

原创 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 453

原创 SICP 习题2.39 reverse实现

(define reverse-list (lambda (s) (if (null? s) '() (append (reverse-list (cdr s)) (list (car s))))))(newline)(display (reverse-list '(1 2 3 4))) (newline)(display (reverse '(1 2 3

2016-06-24 18:40:07 410

原创 SICP 习题2.38 fold-left fold-right

要使fold-left和fold-right达到同样的结果,要求op对操作顺序无关,譬如+(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))))(define

2016-06-24 17:39:59 335

原创 SICP 习题2.37 矩阵乘法的一些实现

(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))))(define (accumulate-n op init seqs) (if (null? (car

2016-06-24 17:24:36 476

原创 SICP 习题2.36 accumulate-n的实现

(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))))(define (accumulate-n op init seqs) (if (null? (car

2016-06-24 17:22:36 581

原创 SICP 习题2.35 count-leaves 用accumulate实现

这题开始我想着用emumerate-tree来做,但是这样做根本就不需要accumulate,后来上网看了别人的做法,我对于递归的理解实在是太浅显。(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initi

2016-06-24 16:41:56 536

原创 SICP 练习2.34 多项式求值(horner规则)

(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))))(define (horner-eval x coefficient-sequence) (accum

2016-06-24 16:13:50 578

原创 SICP 习题2.33 用accumulate完成一些基本的表操作

这个accumulate比第一章完成更强大(define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence)))))(define (map p sequence) (accu

2016-06-24 16:05:34 630

原创 SICP 习题2.32 subsets 寻找子集

写代码的时候遇到一个奇葩的问题,(if (null? s) '() ...)开始我是这样写的,然后发现这样写了之后,map不认账,map rest的时候直接不处理空列表,然后就返回一个()了。(define (subsets s) (if (not (null? s)) (display (car s))) (if (null? s) '(nil) (let

2016-06-24 15:21:06 433

原创 SICP 练习2.31 tree-map

(define (tree-map proc tree) (map (lambda (sub-tree) (cond ((null? sub-tree) ()) ((not (pair? sub-tree)) (proc sub-tree)) (else (tree-map proc sub-tree))))

2016-06-24 14:57:54 296

原创 SICP 练习2.30 square-tree

普通递归写法(define (square-tree tree) (cond ((null? tree) ()) ((not (pair? tree)) (* tree tree)) (else (cons (square-tree (car tree)) (square-tree (cdr tree))))))(squar

2016-06-24 14:47:22 359

原创 SICP 习题2.29 二叉活动体

list版本(define (make-mobile left right) (list left right))(define (make-branch length stucture) (list length stucture))(define (left-branch x) (car x))(define (right-branch x) (car (cdr x)))(def

2016-06-24 14:20:12 426

原创 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 403

原创 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 362

原创 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 354

原创 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 363

原创 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 330

原创 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 449

原创 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 1092

原创 SICP 习题2.18 reverse-list

(define reverse-list (lambda (s) (if (null? (cdr s)) (car s) (cons (reverse-list (cdr s)) (car s)))))(reverse-list '(1 4 9 16 25))

2016-06-12 18:15:11 339

原创 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 388

原创 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 514

原创 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 430

原创 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 402

原创 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 426

原创 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 512

原创 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 567

原创 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 930

原创 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 430

原创 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 536

原创 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 576

原创 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 722

原创 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 435

原创 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 449

原创 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 322

原创 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 372

原创 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 427

原创 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 296

原创 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 478

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除