aboutsummaryrefslogtreecommitdiff
path: root/SICP
diff options
context:
space:
mode:
Diffstat (limited to 'SICP')
-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)