`
t0uch
  • 浏览: 56796 次
  • 性别: Icon_minigender_1
  • 来自: 柳州
社区版块
存档分类
最新评论
文章列表

SICP 2.1 答案

    博客分类:
  • SICP
(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)) ...

SICP 1.37 答案

    博客分类:
  • SICP
经过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))

SICP 1.33 答案

    博客分类:
  • SICP
我的代码有点丑陋。。。不过算了,练习而已 (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) ...

SICP 1.32 答案

    博客分类:
  • SICP
从之前写好的代码可以很容易写出 递归版 (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) ...

SICP 1.31 答案

    博客分类:
  • SICP
递归的版本 (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 ...

SICP 1.30 答案

    博客分类:
  • SICP
没有什么好解释,直接看代码 (define (sum term a next b) (define (iter a result) (if (> a b) result (iter (next a) (+ (term a) result)))) (iter 0 0))
感觉做这个题目是要形成一种思想,这个算法很牛,比书题目上面的那个算法精确多了。 (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 ( ...
最新版的PLT Scheme默认的语言列表里并没有能完全兼容SICP习题中的Scheme,所以需要进行一些调整。我用的版本是4.22,可能别的版本和这个有些许差别。 点击左下角"Choose Languages",然后点击"Show Details",在"Automatic #lang line"中输入: #lang planet neil/sicp 后点击OK 即可。稍等片刻,有Finish字样出现后关掉PLT Scheme后重新启动。看看Language里是不是有SICP了?可以直接用了。

SICP 1.28 答案

    博客分类:
  • SICP
其实,只要做一点点改写就可以达到目的了。 (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) ...

SICP 1.25 1.26 答案

    博客分类:
  • SICP
1.25 如果如Alyssa P. Hacker所说,那收敛的速度很慢,效率会很低的。 1.26 这个怎么说呢,一个square操作做了两次迭代,O(log n)肯定变成O(n)了
这算法,太猛了,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 ( ...

SICP 1.23 答案

    博客分类:
  • SICP
这方法确实会有所改善,大概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-divi ...

SICP 1.22 答案

    博客分类:
  • SICP
程序写出来了,但是时间没打印,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) ...

SICP 1.21 答案

    博客分类:
  • SICP
很简单 (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)) ...
终于把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 - ma ...
Global site tag (gtag.js) - Google Analytics