;double的功能是2*n
(define (double n)
(+ n n))
(double 5)
;*函数的功能是a乘以b
(define (* a b)
(if (= b 0)
0
(+ a (* a (- b 1)))))
(* 3 4)
;havle-iter函数的功能是通过迭代获得n的一半的值,guess是初始猜测值
(define (havle-iter guess n)
(cond ((not (integer? n)) (display "error: 原因havle函数的参数是整数\n"))
(( and (< (double guess) n) (<= (double (+ guess 1)) n)) (havle-iter (+ 1 guess) n))
(( and (> (double guess) n)) (havle-iter (- guess 1) n))
(( and (< (double guess) n) (> (double (+ guess 1)) n)) (+ guess 0.5))
((= (double guess) n) guess)))
;havle函数的功能是通过迭代获得n的一半的值,guess是初始猜测值是1
(define (halve n)
(havle-iter 1 n))
(halve 16)
(halve 37)
(halve 35.5)
;even?函数功能是判断n是否是偶数,如果是偶数值为#t,否则为#f
(define (even? n)
(cond ((= n 0) #t)
((= n 1) #f)
((and (integer? n) (< n 0)) (even? (+ n 2)))
((and (integer? n) (> n 1)) (even? (- n 2)))
(else #f)))
(even? 13)
(even? 22)
(even? 22.5)
;square函数的功能是得到n的平方,如果是非零正偶数则递归计算4倍二分之n的平方
(define (square n)
(cond ((= n 0) 0)
((< n 0) (square (- 0 n)))
((and (> n 0) (even? n)) (double (double (square (halve n)))))
((> n 0) (+ (square (- n 1)) (double (- n 1)) 1))))
(square 7)
(square 12)
(define (fast-expt b n)
(cond ((= 0 n) 1)
((even? n) (fast-expt (square b) (halve n)))
(else (* b (fast-expt b (- n 1))))))
(fast-expt 2 10)
分享到:
相关推荐
西普 我的 SICP 练习。
NULL 博文链接:https://pengpeng.iteye.com/blog/1344689
SICP习题解答,主要第一章的内容习题答案
SICP 习题答案 计算机程序的构造和解释 1-3章 习题答案
sicp-clojure 在 Clojure 中解决的 SICP 练习
sicp 我对SICP练习的回答
sicp in python 中文版 sicp in python 中文版 sicp in python 中文版 !!!download>>>https://github.com/wizardforcel/sicp-py-zh
SICP 在 Scheme 中制定的 SICP 练习
SICP-解决方案我对 SICP 练习的回答
SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版
sicp
SICP-Python版本
SICP 使用的scheme解释器 以前叫DrScheme
SICP书中练习的解决方案。 使用mit-scheme编译器9.2。
sicp 2.2.4节图形语言的racket程序包,配置路径,C:\Users\Administrator\AppData\Roaming\Racket
SICP - 笔记和练习我把它放在这里是因为有一天它可能会帮助某人。 练习是ex*文件。 章节中的注释和代码是ch文件。安装下载 Racket.app。 使用 DrRacket.app 或像这样启动 Racket repl: /Applications/Racket\ v...
SICP 解题集
Python SICP epub版本,很适合学习抽象的思想,用Python版本比lisp更实用