ix 8 years ago
commit
15092c9259
  1. 42
      ch1/constructing-procedures-using-lambda.scm
  2. 21
      ch1/counting-change.scm
  3. 58
      ch1/ex1.scm
  4. 45
      ch1/ex10.scm
  5. 17
      ch1/ex11.scm
  6. 4
      ch1/ex12.scm
  7. 25
      ch1/ex15.scm
  8. 18
      ch1/ex16.scm
  9. 13
      ch1/ex17.scm
  10. 12
      ch1/ex18.scm
  11. 18
      ch1/ex19.scm
  12. 3
      ch1/ex2.scm
  13. 44
      ch1/ex22.scm
  14. 49
      ch1/ex23.scm
  15. 59
      ch1/ex24.scm
  16. 27
      ch1/ex29.scm
  17. 6
      ch1/ex3.scm
  18. 33
      ch1/ex30.scm
  19. 37
      ch1/ex31.scm
  20. 36
      ch1/ex32.scm
  21. 39
      ch1/ex33.scm
  22. 6
      ch1/ex34.scm
  23. 4
      ch1/ex4.scm
  24. 6
      ch1/ex5.scm
  25. 38
      ch1/ex6.scm
  26. 22
      ch1/ex7.scm
  27. 21
      ch1/ex8.scm
  28. 49
      ch1/ex9.scm
  29. 19
      ch1/exponentiation.scm
  30. 6
      ch1/gcd.scm
  31. 54
      ch1/procedures-as-arguments.scm
  32. 37
      ch1/testing-for-primality.scm

42
ch1/constructing-procedures-using-lambda.scm

@ -0,0 +1,42 @@
(define (sum term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (+ result (term a)))))
(iter a 0))
(define (pi-sum a b)
(sum (lambda (x) (/ 1.0 (* x (+ x 2))))
a
(lambda (x) (+ x 4))
b))
(define (integral f a b dx)
(* (sum f
(+ a (/ dx 2.0))
(lambda (x) (+ x dx))
b)
dx))
;(define (f x y)
; (define (f-helper a b)
; (+ (* x (square a))
; (* y b)
; (* a b)))
; (f-helper (+ 1 (* x y))
; (- 1 y)))
;(define (f x y)
; ((lambda (a b)
; (+ (* x (square a))
; (* y b)
; (* a b)))
; (+ 1 (* x y))
; (- 1 y)))
(define (f x y)
(let ((a (+ 1 (* x y)))
(b (- 1 y)))
(+ (* x (square a))
(* y b)
(* a b))))

21
ch1/counting-change.scm

@ -0,0 +1,21 @@
(define (cc amount kinds-of-coins)
(cond ((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)
(else (+ (cc amount
(- kinds-of-coins 1))
(cc (- amount
(first-denomination
kinds-of-coins))
kinds-of-coins)))))
(define (count-change amount) (cc amount 5))
(define (first-denomination kinds-of-coins)
(cond ((= kinds-of-coins 1) 1)
((= kinds-of-coins 2) 5)
((= kinds-of-coins 3) 10)
((= kinds-of-coins 4) 25)
((= kinds-of-coins 5) 50)))
(display (count-change 100))
(newline)

58
ch1/ex1.scm

@ -0,0 +1,58 @@
10
;; => 10
(+ 5 3 4)
;; => 12
(- 9 1)
;; => 8
(/ 6 2)
;; => 3
(+ (* 2 4) (- 4 6))
;; => 6
(define a 3)
;; => n/a; a = 3
(define b (+ a 1))
;; => n/a; b = 4
(+ a b (* a b))
;; => 19
(= a b)
;; => #f
(if (and (> b a) (< b (* a b)))
b
a)
;; => 4
(cond ((= a 4) 6)
((= b 4) (+ 6 7 a))
(else 25))
;; => 16
(+ 2 (if (> b a) b a))
;; => 6
(* (cond ((> a b) a)
((< a b) b)
(else -1))
(+ a 1))
;; => 16

45
ch1/ex10.scm

@ -0,0 +1,45 @@
(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 0 (A 1 9))
;; -> (A 0 (A 0 (A 1 8))
;; -> (A 0 (A 0 (A 0 (A 1 7))))
;; -> (A 0 (A 0 (A 0 (A 0 (A 1 6)))))
;; -> (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 5))))))
;; -> (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 4)))))))
;; -> (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 3))))))))
;; -> (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 2)))))))))
;; -> (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 1))))))))))
;; -> (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 2)))))))))
;; -> (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 4))))))))
;; -> (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 8)))))))
;; -> (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 16))))))
;; -> (A 0 (A 0 (A 0 (A 0 (A 0 32)))))
;; -> (A 0 (A 0 (A 0 (A 0 64))))
;; -> (A 0 (A 0 (A 0 128)))
;; -> (A 0 (A 0 256))
;; -> (A 0 512)
;; => 1024
(A 2 4)
;; => 65536
;; I'm not going to bother evaluating these by hand anymore.
(A 3 3)
;; => 65536
(define (f n) (A 0 n))
;; 2n
(define (g n) (A 1 n))
;; 2^n
(define (h n) (A 2 n))
;; 2^2^(n-1 times)
(define (k n) (* 5 n n))
;; 5n^2

17
ch1/ex11.scm

@ -0,0 +1,17 @@
(define (f-recur n)
(cond ((< n 3) n)
(else (+ (f-recur (- n 1))
(* 2 (f-recur (- n 2)))
(* 3 (f-recur (- n 3)))))))
(define (f-iter n) (iter 0 1 2 n))
(define (iter a b c count)
(if (= count 0)
a
(iter b c (+ c (* 2 b) (* 3 a)) (- count 1))))
(display (f-recur 7))
(newline)
(display (f-iter 7))
(newline)

4
ch1/ex12.scm

@ -0,0 +1,4 @@
(define (pascal a b)
(cond ((< a b) #f)
((or (= 0 b) (= a b)) 1)
(else (+ (pascal (- a 1) b) (pascal (- a 1) (- b 1))))))

25
ch1/ex15.scm

@ -0,0 +1,25 @@
(define (cube x) (* x x x))
(define (p x) (- (* 3 x) (* 4 (cube x))))
(define (sine ang)
(if (not (> (abs ang) 0.1))
ang
(p (sine (/ ang 3.0)))))
(sine 12.15)
;; -> (p (sine 4.05))
;; -> (p (p (sine 1.35)))
;; -> (p (p (p (sine 0.45))))
;; -> (p (p (p (p (sine 0.15)))))
;; -> (p (p (p (p (p (sine 0.05))))))
;; -> (p (p (p (p (p 0.05)))))
;; -> (p (p (p (p 0.1495))))
;; -> (p (p (p 0.4351345505)))
;; -> (p (p 0.975846533167877))
;; -> (p -0.789563114470821)
;; => -0.399803457413348
;; a: 5
;; b: space: O(log(x))
;; steps: O(log(x))

18
ch1/ex16.scm

@ -0,0 +1,18 @@
(define (square x)
(* x x))
(define (nice-expt b n)
(define (iter a b n)
(cond ((zero? n) a)
((even? n) (iter a (square b) (/ n 2)))
(else (iter (* a b) b (- n 1)))))
(iter 1 b n))
(display (nice-expt 3 2))
(newline)
;; (nice-expt 3 2)
;; -> (iter 1 3 2)
;; -> (iter 1 9 1)
;; -> (iter 9 9 0)
;; => 9

13
ch1/ex17.scm

@ -0,0 +1,13 @@
(define (double x)
(+ x x))
(define (halve x)
(/ x 2))
(define (nice-mult a b)
(cond ((zero? b) 0)
((even? b) (double (nice-mult a (halve b))))
(else (+ a (nice-mult a (- b 1))))))
(display (nice-mult 3 3))
(newline)

12
ch1/ex18.scm

@ -0,0 +1,12 @@
(define (double x)
(+ x x))
(define (halve x)
(/ x 2))
(define (nice-mult a b)
(define (iter acc a b)
(cond ((zero? b) acc)
((even? b) (iter acc (double a) (halve b)))
(else (iter (+ acc a) (double a) (halve (- b 1))))))
(iter 0 a b))

18
ch1/ex19.scm

@ -0,0 +1,18 @@
(define (square x)
(* x x))
(define (fib n)
(define (fib-iter a b p q count)
(cond ((= count 0) b)
((even? count)
(fib-iter a
b
(+ (square p) (square q)) ; compute p'
(+ (* 2 p q) (square q)) ; compute q'
(/ count 2)))
(else (fib-iter (+ (* b q) (* a q) (* a p))
(+ (* b p) (* a q))
p
q
(- count 1)))))
(fib-iter 1 0 0 1 n))

3
ch1/ex2.scm

@ -0,0 +1,3 @@
(/
(+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
(* 3 (- 6 2) (- 2 7)))

44
ch1/ex22.scm

@ -0,0 +1,44 @@
;; chicken scheme doesn't provide a (runtime) primitive,
;; and (current-milliseconds) is the best i could get
(define (square x)
(* x x))
(define (smallest-divisor n)
(find-divisor n 2))
(define (find-divisor n test-divisor)
(cond ((> (square 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))
(define (prime? n)
(= n (smallest-divisor n)))
(define (timed-prime-test n)
(start-prime-test n (current-milliseconds)))
(define (start-prime-test n start-time)
(if (prime? n)
(report-prime n (- (current-milliseconds) start-time))))
(define (search-for-primes min-bound max-bound)
(define (iter cur max-bound)
(if (<= cur max-bound) (timed-prime-test cur))
(if (<= cur max-bound) (iter (+ cur 2) max-bound)))
(iter (if (even? min-bound) (+ min-bound 1) min-bound)
(if (even? max-bound) (+ max-bound 1) max-bound)))
(define (report-prime n elapsed-time)
(display n)
(display " *** ")
(display elapsed-time)
(newline))
;; 1009 0.0, 1013 0.0, 1019 0.0
;; 10007 0.0, 10009 1.0, 10037 0.0
;; 100003 2.0, 100019 1.0, 100043 1.0
;; 1000003 3.0, 1000033 4.0, 1000037 4.0

49
ch1/ex23.scm

@ -0,0 +1,49 @@
;; chicken scheme doesn't provide a (runtime) primitive,
;; and (current-milliseconds) is the best i could get
(define (square x)
(* x x))
(define (smallest-divisor n)
(find-divisor n 2))
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (next test-divisor)))))
(define (divides? a b)
(= (remainder b a) 0))
(define (next a)
(if (= 2 a)
3
(+ a 2)))
(define (prime? n)
(= n (smallest-divisor n)))
(define (timed-prime-test n)
(start-prime-test n (current-milliseconds)))
(define (start-prime-test n start-time)
(if (prime? n)
(report-prime n (- (current-milliseconds) start-time))))
(define (search-for-primes min-bound max-bound)
(define (iter cur max-bound)
(if (<= cur max-bound) (timed-prime-test cur))
(if (<= cur max-bound) (iter (+ cur 2) max-bound)))
(iter (if (even? min-bound) (+ min-bound 1) min-bound)
(if (even? max-bound) (+ max-bound 1) max-bound)))
(define (report-prime n elapsed-time)
(display n)
(display " *** ")
(display elapsed-time)
(newline))
;; 1009 1.0, 1013 0.0, 1019 0.0
;; 10007 0.0, 10009 0.0, 10037 0.0
;; 100003 1.0, 100019 0.0, 100043 0.0
;; 1000003 2.0, 1000033 2.0, 1000037 2.0

59
ch1/ex24.scm

@ -0,0 +1,59 @@
;; chicken scheme doesn't provide a (runtime) primitive,
;; and (current-milliseconds) is the best i could get
(define (square x)
(* x x))
(define (exptmod base expn m)
(cond ((= expn 0) 1)
((even? expn)
(remainder
(square (exptmod base (/ expn 2) m))
m))
(else
(remainder
(* base (exptmod base (- expn 1) m))
m))))
(define (fermat-test n)
(define (try-it a)
(= (exptmod a n n) a))
(try-it (+ 1 (random (- n 1)))))
(define (fast-prime? n times)
(cond ((= times 0) #t)
((fermat-test n) (fast-prime? n (- times 1)))
(else #f)))
(define (timed-prime-test n)
(start-prime-test n (current-milliseconds)))
(define (start-prime-test n start-time)
(if (fast-prime? n 10)
(report-prime n (- (current-milliseconds) start-time))))
(define (search-for-primes min-bound max-bound)
(define (iter cur max-bound)
(if (<= cur max-bound) (timed-prime-test cur))
(if (<= cur max-bound) (iter (+ cur 2) max-bound)))
(iter (if (even? min-bound) (+ min-bound 1) min-bound)
(if (even? max-bound) (+ max-bound 1) max-bound)))
(define (report-prime n elapsed-time)
(display n)
(display " *** ")
(display elapsed-time)
(newline))
;; 1009 1.0, 1013 0.0, 1019 0.0
;; 10007 1.0, 10009 0.0, 10037 1.0
;; 100003 3.0, 100019 1.0, 100043 1.0
;; 1000003 1.0, 1000033 1.0, 1000037 1.0
;; the discrepancy in this particular implementation is caused by
;; the fact that no matter what, you're testing it 10, 50, 100 times;
;; obviously it'll be slower with a large number of times to run,
;; no matter how efficient the algorithm is.
;;
;; the other implementation doesn't have this problem, due to the
;; way it's implemented.

27
ch1/ex29.scm

@ -0,0 +1,27 @@
(define (cube x)
(* x x x))
(define (inc x)
(+ x 1))
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
(define (integral f a b dx)
(define (add-dx x)
(+ x dx))
(* (sum f (+ a (/ dx 2.0)) add-dx b)
dx))
(define (simpson f a b n)
(define h (/ (- b a) n))
(define (yk k) (f (+ a (* h k))))
(define (simpson-term k)
(* (cond ((or (= k 0) (= k n)) 1)
((odd? k) 4)
(else 2))
(yk k)))
(* (/ h 3.0) (sum simpson-term 0 inc n)))

6
ch1/ex3.scm

@ -0,0 +1,6 @@
(define (square x) (* x x))
(define (take-three x y z)
(+ (square x) (square y)))
(display (take-three 3 3 3))

33
ch1/ex30.scm

@ -0,0 +1,33 @@
(define (cube x)
(* x x x))
(define (inc x)
(+ x 1))
(define (sum term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (+ result (term a)))))
(iter a 0))
; after here this is all copied from example
; 29, it's unimportant
;
; i also don't understand integral calculus
(define (integral f a b dx)
(define (add-dx x)
(+ x dx))
(* (sum f (+ a (/ dx 2.0)) add-dx b)
dx))
(define (simpson f a b n)
(define h (/ (- b a) n))
(define (yk k) (f (+ a (* h k))))
(define (simpson-term k)
(* (cond ((or (= k 0) (= k n)) 1)
((odd? k) 4)
(else 2))
(yk k)))
(* (/ h 3.0) (sum simpson-term 0 inc n)))

37
ch1/ex31.scm

@ -0,0 +1,37 @@
(define (cube x)
(* x x x))
(define (inc x)
(+ x 1))
(define (identity x) x)
(define (sum term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (+ result (term a)))))
(iter a 0))
(define (product term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* result (term a)))))
(iter a 1))
(define (product-rec term a next b)
(if (> a b)
0
(* (term a)
(product-rec term (next a) next b))))
(define (factorial n)
(product identity 1 inc n))
(define (nice-pi n)
(define (pi-term n)
(if (even? n)
(/ (+ n 2) (+ n 1))
(/ (+ n 1) (+ n 2))))
(* (product pi-term 1 inc n) 4))

36
ch1/ex32.scm

@ -0,0 +1,36 @@
(define (cube x)
(* x x x))
(define (inc x)
(+ x 1))
(define (identity x) x)
(define (accumulate combiner null-value term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (combiner result (term a)))))
(iter a null-value))
(define (accumulate-rec combiner null-value term a next b)
(if (> a b)
null-value
(combiner (term a)
(accumulate-rec term (next a) next b))))
(define (sum term a next b)
(accumulate + 0 term a next b))
(define (product term a next b)
(accumulate * 1 term a next b))
(define (factorial n)
(product identity 1 inc n))
(define (nice-pi n)
(define (pi-term n)
(if (even? n)
(/ (+ n 2) (+ n 1))
(/ (+ n 1) (+ n 2))))
(* (product pi-term 1 inc n) 4))

39
ch1/ex33.scm

@ -0,0 +1,39 @@
(define (cube x)
(* x x x))
(define (inc x)
(+ x 1))
(define (identity x) x)
(define (filtered-accumulate combiner null-value term a next b predicate)
(define (iter a result)
(cond ((> a b) result)
((predicate a) (iter (next a) (combiner result (term a))))
(else (iter (next a) result))))
(iter a null-value))
(define (square x)
(* x x))
(define (smallest-divisor n)
(find-divisor n 2))
(define (find-divisor n test-divisor)
(cond ((> (square 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))
(define (prime? n)
(= n (smallest-divisor n)))
(define (sum-sq-prime a b)
(filtered-accumulate + 0 square a inc b prime?))
(define (prod-relative-prime n)
(define (relative-prime? a)
(= 1 (gcd a n)))
(filtered-accumulate * 1 identity 1 inc n relative-prime?))

6
ch1/ex34.scm

@ -0,0 +1,6 @@
(define (f g) (g 2))
;; (f f)
;; -> (f 2)
;; -> (2 2)
;; => 2 is not a procedure

4
ch1/ex4.scm

@ -0,0 +1,4 @@
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
(display (a-plus-abs-b 3 3))

6
ch1/ex5.scm

@ -0,0 +1,6 @@
(define (p) (p))
(define (test x y)
(if (= x 0) 0 y))
(test 0 (p))

38
ch1/ex6.scm

@ -0,0 +1,38 @@
(define (square x) (* x x))
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(define (sqrt x)
(sqrt-iter 1.0 x))
;;; this is where things begin fucking up
;;; it doesn't work because if is a special case
;;; that uses lazy evaluation, thus it doesn't have to
;;; evaluate its parameters.
;;;
;;; when testing, i found that in guile, it caused a stack overflow;
;;; whereas in chicken, it caused an infinite loop (maybe).
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
(define (new-sqrt-iter guess x)
(new-if (good-enough? guess x)
guess
(new-sqrt-iter (improve guess x) x)))
(display (sqrt 3))
;;(display (new-sqrt-iter 1.0 2))

22
ch1/ex7.scm

@ -0,0 +1,22 @@
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (good-enough? guess old)
(< (abs (- guess old)) 0.001))
(define (sqrt-iter guess old x)
(if (good-enough? guess old)
guess
(sqrt-iter (improve guess x) guess x)))
(define (my-sqrt x)
(sqrt-iter 1.0 x x))
;; floating point inaccuracies are the big player here
(display (my-sqrt 10000000000000))
(display " ")
(display (my-sqrt 0.0000003))

21
ch1/ex8.scm

@ -0,0 +1,21 @@
(define (square x) (* x x))
(define (cube x) (* x x x))
;; (x / y ^ 2 + 2y) / 3
(define (improve y x)
(/ (+ (/ x (square y)) (* 2 y)) 3))
(define (good-enough? y old)
(< (abs (- y old)) 0.001))
(define (cbrt-iter y old x)
(if (good-enough? y old)
y
(cbrt-iter (improve y x) y x)))
(define (my-cbrt x)
(cbrt-iter 1.0 x x))
(display (my-cbrt 9))

49
ch1/ex9.scm

@ -0,0 +1,49 @@
;; setup
(define (inc a)
(+ a 1))
(define (dec a)
(- a 1))
;; my-plus 1
(define (my-plus a b)
(if (= a 0) b (inc (my-plus (dec a) b))))
(my-plus 4 5)
;; -> (my-plus 4 5)
;; -> (inc (my-plus (dec 4) 5))
;; -> (inc (my-plus 3 5))
;; -> (inc (inc (my-plus (dec 3) 5)))
;; -> (inc (inc (my-plus 2 5)))
;; -> (inc (inc (inc (my-plus (dec 2) 5))))
;; -> (inc (inc (inc (my-plus 1 5))))
;; -> (inc (inc (inc (inc (my-plus (dec 1) 5)))))
;; -> (inc (inc (inc (inc (my-plus 0 5)))))
;; -> (inc (inc (inc (inc 5))))
;; -> (inc (inc (inc 6)))
;; -> (inc (inc 7))
;; -> (inc 8)
;; => 9
;; a: recursive process
;; my-plus 2
(define (my-plus-again)
(if (= a 0) b (my-plus-again (dec a) (inc b))))
(my-plus-again 4 5)
;; -> (my-plus-again 4 5)
;; -> (my-plus-again (dec 4) (inc 5))
;; -> (my-plus-again 3 6)
;; -> (my-plus-again (dec 3) (inc 6))
;; -> (my-plus-again 2 7)
;; -> (my-plus-again (dec 2) (inc 7))
;; -> (my-plus-again 1 8)
;; -> (my-plus-again (dec 1) (inc 8))
;; -> (my-plus-again 0 9)
;; => 9
;; a: iterative process

19
ch1/exponentiation.scm

@ -0,0 +1,19 @@
(define (my-expt-recur b n)
(if (= n 0)
1
(* b (my-expt-recur b (- n 1)))))
(define (my-expt-iter b n)
(define (iter b n product)
(if (= n 0)
product
(iter b
(- n 1)
(* b product))))
(iter b n 1))
(define (fast-expt b n)
(define (square x) (* x x))
(cond ((= n 0) 1)
((even? n) (square (fast-expt b (/ n 2))))
(else (* b (fast-expt b (- n 1))))))

6
ch1/gcd.scm

@ -0,0 +1,6 @@
;; nice!
(define (nice-gcd a b)
(if (= b 0)
a
(nice-gcd b (remainder a b))))

54
ch1/procedures-as-arguments.scm

@ -0,0 +1,54 @@
(define (cube x)
(* x x x))
;(define (sum-integers a b)
; (if (> a b)
; 0
; (+ a (sum-integers (+ a 1) b))))
;
;(define (sum-cubes a b)
; (if (> a b)
; 0
; (+ a (sum-integers (+ a 1) b))))
;
;(define (pi-sum a b)
; (if (> a b)
; 0
; (+ (/ 1.0 (* a (+ a 2)))
; (pi-sum (+ a 4) b))))
;; (define (<name> a b)
;; (if (> a b)
;; 0
;; (+ (<term> a)
;; (<name> (<next> a) 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))
(define (identity x) x)
(define (sum-integers a b)
(sum identity a inc b))
(define (pi-sum a b)
(define (pi-term x)
(/ 1.0 (* x (+ x 2))))
(define (pi-next x)
(+ x 4))
(sum pi-term a pi-next b))
(define (integral f a b dx)
(define (add-dx x)
(+ x dx))
(* (sum f (+ a (/ dx 2.0)) add-dx b)
dx))

37
ch1/testing-for-primality.scm

@ -0,0 +1,37 @@
(define (square x)
(* x x))
(define (smallest-divisor n)
(find-divisor n 2))
(define (find-divisor n test-divisor)
(cond ((> (square 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))
(define (prime? n)
(= n (smallest-divisor n)))
(define (exptmod base expn m)
(cond ((= expn 0) 1)
((even? expn)
(remainder
(square (exptmod base (/ expn 2) m))
m))
(else
(remainder
(* base (exptmod base (- expn 1) m))
m))))
(define (fermat-test n)
(define (try-it a)
(= (exptmod a n n) a))
(try-it (+ 1 (random (- n 1)))))
(define (fast-prime? n times)
(cond ((= times 0) #t)
((fermat-test n) (fast-prime? n (- times 1)))
(else #f)))
Loading…
Cancel
Save