commit 7c75afe0425d350ac1db2cd09c499145a851ccfc
parent 4a763364f95f99c0a6597d5f9a8049a741a5d048
Author: Yuval Langer <yuval.langer@gmail.com>
Date: Mon, 10 Apr 2023 21:26:04 +0300
Add solutions to exercise 1.32 and exercise 1.33.
Diffstat:
7 files changed, 218 insertions(+), 8 deletions(-)
diff --git a/sicp/solutions/1_21.scm b/sicp/solutions/1_21.scm
@@ -3,7 +3,13 @@
(import (srfi srfi-1))
(import (srfi srfi-64))
(import (only (guile) random))
- (export prime? expmod fast-prime? smallest-divisor divides?)
+ (export
+ prime?
+ expmod
+ fast-prime?
+ smallest-divisor
+ divides?
+ )
;; XXX
diff --git a/sicp/solutions/1_31.scm b/sicp/solutions/1_31.scm
@@ -11,7 +11,7 @@
(define (identity x) x)
(define (1+ x) (+ 1 x))
(define (1- x) (- 1 x))
-
+
(define (iterative-product term a next b)
(define (iter a result)
(if (> a b)
diff --git a/sicp/solutions/1_32.scm b/sicp/solutions/1_32.scm
@@ -0,0 +1,67 @@
+(define-library (sicp solutions 1_32)
+ (import (scheme base))
+ (export
+ iterative-accumulate
+ iterative-product
+ iterative-sum
+ recursive-accumulate
+ recursive-product
+ recursive-sum
+ )
+
+ (begin
+ (define (iterative-accumulate combiner null-value term a next b)
+ (define (iter a result)
+ (if (> a b)
+ result
+ (iter (next a) (combiner (term a) result))))
+ (iter a null-value))
+
+ (define (recursive-accumulate combiner null-value term a next b)
+ (if (> a b)
+ null-value
+ (combiner (term a)
+ (recursive-accumulate combiner
+ null-value
+ term
+ (next a)
+ next
+ b))))
+
+ (define iterative-sum
+ (lambda (term a next b)
+ (iterative-accumulate (lambda (acc x)
+ (+ acc x))
+ 0
+ term
+ a
+ next
+ b)))
+ (define iterative-product
+ (lambda (term a next b)
+ (iterative-accumulate (lambda (acc x)
+ (* acc x))
+ 1
+ term
+ a
+ next
+ b)))
+
+ (define recursive-sum
+ (lambda (term a next b)
+ (recursive-accumulate (lambda (acc x)
+ (+ acc x))
+ 0
+ term
+ a
+ next
+ b)))
+ (define recursive-product
+ (lambda (term a next b)
+ (recursive-accumulate (lambda (acc x)
+ (* acc x))
+ 1
+ term
+ a
+ next
+ b)))))
diff --git a/sicp/solutions/1_33.scm b/sicp/solutions/1_33.scm
@@ -0,0 +1,54 @@
+(define-library (sicp solutions 1_33)
+ (import (scheme base))
+ (import (only (sicp solutions 1_21) prime?))
+ (export
+ iterative-filtered-accumulate
+ iterative-sum-of-squares
+ recursive-filtered-accumulate
+ recursive-sum-of-squares
+ )
+
+ (begin
+ (define (iterative-filtered-accumulate predicate? combiner null-value term a next b)
+ (define (iter a result)
+ (if (> a b)
+ result
+ (if (predicate? a)
+ (iter (next a)
+ (combiner (term a)
+ result))
+ (iter (next a)
+ result))))
+ (iter a null-value))
+
+ (define (iterative-sum-of-squares a b)
+ (iterative-filtered-accumulate prime?
+ +
+ 0
+ (lambda (x) (* x x))
+ a
+ (lambda (x) (+ 1 x))
+ b))
+
+ (define (recursive-filtered-accumulate predicate? combiner null-value term a next b)
+ (if (> a b)
+ null-value
+ (let ([recur (recursive-filtered-accumulate predicate?
+ combiner
+ null-value
+ term
+ (next a)
+ next
+ b)])
+ (if (predicate? a)
+ (combiner (term a) recur)
+ recur))))
+
+ (define (recursive-sum-of-squares a b)
+ (iterative-filtered-accumulate prime?
+ +
+ 0
+ (lambda (x) (* x x))
+ a
+ (lambda (x) (+ 1 x))
+ b))))
diff --git a/sicp/tests/1_30.scm b/sicp/tests/1_30.scm
@@ -7,11 +7,20 @@
(test-begin "1.30")
(test-equal
(iterative-sum (lambda (x) (* x x))
- 0
- (lambda (x) (+ 1 x))
- 10)
+ 5
+ (lambda (x) (+ 2 x))
+ 20)
(linear-recursive-sum (lambda (x) (* x x))
- 0
- (lambda (x) (+ 1 x))
- 10))
+ 5
+ (lambda (x) (+ 2 x))
+ 20))
+ (test-equal
+ (iterative-sum (lambda (x) (* x x))
+ 5
+ (lambda (x) (* 2 x))
+ 200)
+ (linear-recursive-sum (lambda (x) (* x x))
+ 5
+ (lambda (x) (* 2 x))
+ 200))
(test-end "1.30")))
diff --git a/sicp/tests/1_32.scm b/sicp/tests/1_32.scm
@@ -0,0 +1,48 @@
+(define-library (sicp tests 1_32)
+ (import (scheme base))
+ (import (srfi :64))
+ (import (prefix (sicp solutions 1_30) 1_30:))
+ (import (prefix (sicp solutions 1_31) 1_31:))
+ (import (prefix (sicp solutions 1_32) 1_32:))
+
+ (begin
+ (test-begin "1.32")
+ (test-equal
+ (1_30:iterative-sum (lambda (x) (* x x))
+ 5
+ (lambda (x) (* 2 x))
+ 20)
+ (1_32:iterative-sum (lambda (x) (* x x))
+ 5
+ (lambda (x) (* 2 x))
+ 20)
+ )
+ (test-equal
+ (1_30:iterative-sum (lambda (x) (* x x))
+ 5
+ (lambda (x) (* 2 x))
+ 20)
+ (1_32:recursive-sum (lambda (x) (* x x))
+ 5
+ (lambda (x) (* 2 x))
+ 20)
+ )
+ (test-equal
+ (1_31:iterative-product (lambda (x) (* x x))
+ 5
+ (lambda (x) (* 2 x))
+ 20)
+ (1_32:iterative-product (lambda (x) (* x x))
+ 5
+ (lambda (x) (* 2 x))
+ 20))
+ (test-equal
+ (1_31:iterative-product (lambda (x) (* x x))
+ 5
+ (lambda (x) (* 2 x))
+ 20)
+ (1_32:recursive-product (lambda (x) (* x x))
+ 5
+ (lambda (x) (* 2 x))
+ 20))
+ (test-end "1.32")))
diff --git a/sicp/tests/1_33.scm b/sicp/tests/1_33.scm
@@ -0,0 +1,26 @@
+(define-library (sicp tests 1_32)
+ (import (scheme base))
+ (import (srfi :64))
+ (import (sicp solutions 1_33))
+
+ (begin
+ (test-begin "1.33")
+ (test-equal
+ (iterative-filtered-accumulate odd?
+ +
+ 0
+ (lambda (x) (* x x))
+ 0
+ (lambda (x) (+ 1 x))
+ 100)
+ (recursive-filtered-accumulate odd?
+ +
+ 0
+ (lambda (x) (* x x))
+ 0
+ (lambda (x) (+ 1 x))
+ 100))
+ (test-equal
+ (iterative-sum-of-squares 2 100)
+ (recursive-sum-of-squares 2 100))
+ (test-end "1.33")))