随笔分类 - SICP
摘要:(define (double x) (+ x x)) (define (halve x) (/ x 2)) (define (mult a b) (cond ((= b 0) 0) ((even? b) (double (mult a (halve b)))) ((odd? b) (+ a (mult a (- b 1)))))) (mult ...
阅读全文
摘要:(define (fast-expt b n) (expt-iter b n 1)) (define (expt-iter b n a) (cond ((= n 0) a) ((even? n) (expt-iter (square b) (/ n 2) ...
阅读全文
摘要:(define (expte b n) (if (= n 0) 1 (* b (expte b (- n 1))))) ;(expte 2 200000);(expt 2 200000) (define (expta b n) (expta-iter b n 1)) (define (expta-i
阅读全文
摘要:define-values: assignment disallowed; cannot change constant constant: expt 你动了语言内置的函数了. 改个名字吧.
阅读全文
摘要:(define (pascal row col) (cond ((or (< col 0) (< row 0)) (display"behave yourself!")) ((> col row) (display"behave yourself!")) ((or (= col 0) (= col
阅读全文
摘要:递归: (define (f n) (if (< n 3) n (+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3)))))) (f 3) 迭代: (define (f n) (if (< n 3) n (f-iter 0 1 2 n) )) (defin
阅读全文
摘要:(define (first-denomination kinds-of-coin) (cond ((= kinds-of-coin 1) 1) ((= kinds-of-coin 2) 5) ((= kinds-of-coin 3) 10) ((= kinds-of-coin 4) 25) ((=
阅读全文
摘要:树形递归:计算步骤 随输入 指数性 增长,空间 随输入 线性 增长. 线性迭代:计算步骤 随输入 线性 增长,空间 随输入 线性 增长. 用java写出来看看到底有多厉害: 树形递归程序运行时间:70805ms 而线性迭代绝大部分时候都是0ms. (Java里面函数名不允许 - 这个符号)
阅读全文
摘要:(define (A x y) (cond ((= y 0) 0) ((= x 0) (* 2 y)) ((= y 1) 2) (else (A (- x 1) (A x (- y 1)))))) (A 1 10) (A 2 4) (A 3 3) (define (f n) (A 0 n))(f 0
阅读全文
摘要:(define (plus-Recursive a b) (if (= a 0) b (inc (plus-Recursive (dec a) b)))) (define (inc n) (+ n 1)) (define (dec n) (- n 1)) (plus-Recursive 3 5) 从
阅读全文
摘要:递归: (define (factorial n) (if (= n 1) 1 (* n (factorial(- n 1))))) (factorial 3) 迭代: (define (factorial n) (fact-iter 1 1 n)) (define (fact-iter produ
阅读全文
摘要:(define (cube-root x) (cube-root-iter 1.0 x)) (define (cube-root-iter guess x) (if (good-enough? guess x) guess (cube-root-iter (improve guess x) x)))
阅读全文
摘要:(define (sqrt-iter guess x) (if (good-enough? guess (improve guess x)) (improve guess x) (sqrt-iter (improve guess x) x))) (define (improve guess x)(a
阅读全文
摘要:<一> (define (new-if predicate then-clause else-clause) (cond (predicate then-clause) (else else-clause))) (new-if (= 2 3) 0 5)(new-if (= 1 1) 0 5) new
阅读全文
摘要:(define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (define (improve guess x) (average guess (/ x guess)))
阅读全文
摘要:(define (p) (p))(define (test x y) (if (= x 0) 0 y))(test 0 (p)) 哇,这么快就印证了我的想法了吗. 首先用R5RS跑一遍,啥都没有.然后切换成Lazy Racket,结果为0. 解释:前者是应用序,p一传进来就求值,不断的调用自己,无限
阅读全文
摘要:(define (a-plus-abs-b a b) (if (> b 0) (+ a b) (- a b)) )(define (a-plus-abs-b-book a b) ((if (> b 0) + -) a b) )(a-plus-abs-b 1 -1)(a-plus-abs-b-book
阅读全文
摘要:(define (SumOfTwoLarge x y z) (if (and (<= x y) (<= x z)) (+ (* y y) (* z z)) (SumOfTwoLarge y x z)) )(SumOfTwoLarge 0 2 2) 感觉我这个很取巧啊,哈哈哈哈. 我还是有点不习惯把函
阅读全文
摘要:(/ (+ 5 4(- 2 (- 3 (+ 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7))) 注意: (- 1 1 1 1)这样写也可以
阅读全文
摘要:(define a 3) (define b (+ a 1)) b (+ a b(* a b)) (= a b) (if (and (> b a) (< b (* a b))) b a) (cond ((= a 4) 6) ((= b 4) (+ 6 7 a)) (else 25)) (+ 2 (i
阅读全文
浙公网安备 33010602011771号