ITeye博客

蜡笔的涂鸦

t0uch · 共 47 篇文章

SICP 2.1 答案

(define (gcd x y) ;; we've implemented gcd in section 1.2.5. (if (= y 0) x (gcd y (remainder x y)))) (define (signum x) (if (= 0 x) 0 (/ x (abs x)))) (define (make-rat n d) (let ((g (gcd (abs n) (abs d))) (s (signum d))) (cons (/ n (* g s)) (/ d (* g s)))))

2009-12-11 16:44 · SICP · 评论(0)

SICP 1.37 答案

经过8次迭代就能达到精度的要求 递归版 (define (cont-frac n d k) (if (< k 0) 0 (/ (n k) (+ (d k) (cont-frac n d (- k 1)))))) 迭代版 (define (cont-frac n d k) (define (iter k result) (if (< k 0) result (iter (- k 1) (/ (n k) (+ (d k) result))))) (iter k 0))

2009-11-13 17:37 · SICP · 评论(0)

SICP 1.33 答案

我的代码有点丑陋。。。不过算了,练习而已 (define (filtered-accumulate combiner null-value filter term a next b) (define (filter-work x) (if (filter x) (term x) null-value)) (if (> a b) null-value (combiner (filter-work a) (filtered-accumulate combiner null-value filter term (next a) next b)))) a的答案 (define (sum-range-…

2009-11-13 15:16 · SICP · 评论(0)

SICP 1.32 答案

从之前写好的代码可以很容易写出 递归版 (define (accumulate combiner null-value term a next b) (if (> a b) null-value (combiner (term a) (accumulate combiner null-value term (next a) next b)))) 迭代版 (define (accumulate combiner null-value term a next b) (define (iter a result) (if (> a b) result (iter (next a) (combine…

2009-11-13 14:37 · SICP · 评论(0)

SICP 1.31 答案

递归的版本 (define (product term a next b) (if (> a b) 1.0 (* (term a) (product term (next a) next b)))) (define (wallis-product n) (define (square x) (* x x)) (define (inc a) (+ a 2)) (define (term a) (cond ((= a n) 1) ((= a 2) (/ (* 2 n) (square (- n 1)))) (else (/ (square a) (square (- a 1)))))) (* 4…

2009-11-11 19:31 · SICP · 评论(0)

SICP 1.30 答案

没有什么好解释,直接看代码 (define (sum term a next b) (define (iter a result) (if (> a b) result (iter (next a) (+ (term a) result)))) (iter 0 0))

2009-11-11 18:44 · SICP · 评论(0)

SICP 1.29 答案

感觉做这个题目是要形成一种思想,这个算法很牛,比书题目上面的那个算法精确多了。 (define (simpson-intergral f a b n) (define (inc-a x) (+ x 1)) (define (h) (/ (- b a) n)) (define (y x) (+ a (* x (h)))) (define (num x) (cond ((or (= x 0) (= x n)) 1) ((even? x) 2) (else 4))) (define (term x) (* (num x) (f (y x)))) (* (/ (h) 3.0) (sum term 0…

2009-11-11 18:36 · SICP · 评论(0)

PLT Scheme支持SICP的方法

最新版的PLT Scheme默认的语言列表里并没有能完全兼容SICP习题中的Scheme,所以需要进行一些调整。我用的版本是4.22,可能别的版本和这个有些许差别。 点击左下角"Choose Languages",然后点击"Show Details",在"Automatic #lang line"中输入: #lang planet neil/sicp 后点击OK 即可。稍等片刻,有Finish字样出现后关掉PLT Scheme后重新启动。看看Language里是不是有SICP了?可以直接用了。

2009-11-11 10:50 · SICP · 评论(0)

SICP 1.28 答案

其实,只要做一点点改写就可以达到目的了。 (define (square x) (* x x)) (define (nontrivial-check base m r) (cond ((and (not (= r 1)) (not (= r (- m 1))) (= (remainder (square r) m) 1)) 0) (else (square r)))) (define (expmod base exp m) (cond ((= exp 0) 1) ((even? exp) (remainder (nontrivial-check base m (expmod base (/ …

2009-11-11 10:44 · SICP · 评论(0)

SICP 1.25 1.26 答案

1.25 如果如Alyssa P. Hacker所说,那收敛的速度很慢,效率会很低的。 1.26 这个怎么说呢,一个square操作做了两次迭代,O(log n)肯定变成O(n)了

2009-11-10 18:39 · SICP · 评论(0)

SICP 1.24 习题答案

这算法,太猛了,Dr Scheme貌似不是微妙级的。 另外,random貌似不支持比整型还大的数,所以偷懒搞一个最大数的随机数得了,反正费马定律顶着。 (define (square x) (* x x)) (define (expmod base exp m) (cond ((= exp 0) 1) ((even? exp) (remainder (square (expmod base (/ exp 2) m)) m)) (else (remainder (* base (expmod base (- exp 1) m)) m)))) (define (fermat-test n) (d…

2009-11-10 18:28 · SICP · 评论(0)

SICP 1.23 答案

这方法确实会有所改善,大概2倍效率的提升。 (define (smallest-divisor n) (find-divisor n 2)) (define (next n) (if (even? n) (+ n 1) (+ n 2))) (define (find-divisor n test-divisor) (cond ((> (* test-divisor test-divisor) n) n) ((divides? test-divisor n) test-divisor) (else (find-divisor n (next test-divisor))))) (define …

2009-11-10 18:15 · SICP · 评论(0)

SICP 1.22 答案

程序写出来了,但是时间没打印,DrScheme不支持。。。 (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n test-divisor) (cond ((> (* test-divisor test-divisor) n) n) ((divides? test-divisor n) test-divisor) (else (find-divisor n (+ test-divisor 1))))) (define (divides? a b) (= (remainder b a) 0)) (def…

2009-11-10 17:54 · SICP · 评论(0)

SICP 1.21 答案

很简单 (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n test-divisor) (cond ((> (* test-divisor test-divisor) n) n) ((divides? test-divisor n) test-divisor) (else (find-divisor n (+ test-divisor 1))))) (define (divides? a b) (= (remainder b a) 0)) (smallest-divisor 199) ;199 (s…

2009-11-10 16:13 · SICP · 评论(0)

perforce linux启动脚本

终于把perforce的启动脚本搞定,特此纪念一下。 #! /bin/sh # # p4d Start the p4d daemon # # Author: Tony Smith <tony at perforce.com> # # chkconfig: 345 85 05 # description: Starts the Perforce server process # # processname: p4d # daemon - makes rc startup work properly # killproc - makes rc shutdown work properly # S…

2009-10-29 15:51 · 碎碎念 · 评论(0)