aboutsummaryrefslogtreecommitdiff
path: root/SICP/ch1_3.scm
diff options
context:
space:
mode:
authorSteve Lee <me@xiangyangli.com>2017-04-19 22:43:18 +0800
committerSteve Lee <me@xiangyangli.com>2017-04-19 22:43:18 +0800
commit9ee91c759c5a87030cebb6b79adc94230f23da4a (patch)
tree4184793354ac57cdbf7002e6821c301bc24ad44a /SICP/ch1_3.scm
parentce07c94e0ec62a829e0d0c447ec4c932f0f78c3d (diff)
downloadPersonal-9ee91c759c5a87030cebb6b79adc94230f23da4a.tar.xz
Personal-9ee91c759c5a87030cebb6b79adc94230f23da4a.zip
remove
Diffstat (limited to 'SICP/ch1_3.scm')
-rw-r--r--SICP/ch1_3.scm33
1 files changed, 33 insertions, 0 deletions
diff --git a/SICP/ch1_3.scm b/SICP/ch1_3.scm
new file mode 100644
index 0000000..76fadb7
--- /dev/null
+++ b/SICP/ch1_3.scm
@@ -0,0 +1,33 @@
+(define (cube x) (* x x x))
+
+;; Sum from a to b.
+
+(define (sum term a next b)
+ (if (> a b )
+ 0
+ (+ (term a)
+ (sum term (next a) next b))))
+
+(define (inc n) (+ n 1))
+(define (sum-cubes a b)
+ (sum cube a inc b))
+
+(sum-cubes 1 10)
+
+(define (identity x) x)
+(define (sum-integers a b)
+ (sum identity a inc b))
+(sum-integers 1 10)
+
+(define (pi-sum a b)
+ (define (pi-term x)
+ (/ 1.0 (* x (+ x 2))))
+ (define (pi-next x)
+ (+ x 4))
+(* 8 (sum pi-term a pi-next b)))
+(pi-sum 1 1000)
+
+(define (intergral f a b dx)
+ (define (add-dx x) (+ x dx))
+ (* (sum f (+ a (/ dx 2.0)) add-dx b) dx))
+(intergral cube 0 1 0.0001)