learning-sicp

My embarrassing half assed SICP run.
Log | Files | Refs

commit 7119ca02436b98b2c6bc3e11ac73ec1c1a68a8b9
parent e482146b055647ce836e4756cdb0a716ac25e4ac
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Mon, 20 Nov 2023 22:23:50 +0200

Add the new and improved chapter-1 exercise-9 (1.9) solution.

Diffstat:
Msicp/solutions/chapter-1/exercise-9.scm | 123+++++++++++++++++++------------------------------------------------------------
Asicp/tests/chapter-1/exercise-9.scm | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 137 insertions(+), 94 deletions(-)

diff --git a/sicp/solutions/chapter-1/exercise-9.scm b/sicp/solutions/chapter-1/exercise-9.scm @@ -1,103 +1,38 @@ -#! +(define-library (sicp solutions chapter-1 exercise-9) + (import (scheme base)) + (export inc + dec + first-+ + second-+) -*Exercise 1.9:* Each of the following two procedures defines a -method for adding two positive integers in terms of the procedures -`inc', which increments its argument by 1, and `dec', which -decrements its argument by 1. + (begin + (define (inc x) (+ x 1)) + (define (dec x) (- x 1)) -(define (+ a b) - (if (= a 0) - b - (inc (+ (dec a) b)))) + #! -(define (+ a b) - (if (= a 0) - b - (+ (dec a) (inc b)))) + *Exercise 1.9:* Each of the following two procedures defines a + method for adding two positive integers in terms of the procedures + `inc', which increments its argument by 1, and `dec', which + decrements its argument by 1. -Using the substitution model, illustrate the process generated by -each procedure in evaluating `(+ 4 5)'. Are these processes -iterative or recursive? + !# -First: + (define (first-+ a b) + (if (= a 0) + b + (inc (first-+ (dec a) b)))) -(+ 4 5) -(if (= 4 0) - 5 - (inc (+ (dec 4) 5))) -(if #f - 5 - (inc (+ (dec 4) 5))) -(inc (+ (dec 4) 5)) -(inc (+ 3 5)) -(inc (if (= 3 0) - 5 - (inc (+ (dec 3) 5)))) -(inc (if #f - 5 - (inc (+ (dec 3) 5)))) -(inc - (inc (+ (dec 3) 5))) -(inc - (inc (+ 2 5))) -(inc - (inc (if (= 2 0) - 5 - (inc (+ (dec 2) 5))))) -(inc - (inc (if #f - 5 - (inc (+ (dec 2) 5))))) -(inc - (inc - (inc (+ (dec 2) 5)))) -(inc - (inc - (inc (+ 1 5)))) -(inc - (inc - (inc (if (= 1 0) - 5 - (inc (+ (dec 1) 5)))))) -(inc - (inc - (inc (if #f - 5 - (inc (+ (dec 1) 5)))))) -(inc (inc (inc (inc (+ (dec 1) 5))))) -(inc (inc (inc (inc (+ 0 5))))) -(inc (inc (inc (inc (if (= 0 0) 5 (inc (+ (dec 0) 5))))))) -(inc (inc (inc (inc (if #t 5 (inc (+ (dec 0) 5))))))) -(inc (inc (inc (inc 5)))) -(inc (inc (inc 6))) -(inc (inc 7)) -(inc 8) -9 + (define (second-+ a b) + (if (= a 0) + b + (second-+ (dec a) (inc b)))) -Recursive. + #! -Second: + Using the substitution model, illustrate the process generated by + each procedure in evaluating `(+ 4 5)'. Are these processes + iterative or recursive? -(+ 4 5) -(if (= 4 0) 5 (+ (dec 4) (inc 5))) -(if #f 5 (+ (dec 4) (inc 5))) -(+ 3 6) -(if (= 3 0) 6 (+ (dec 3) (inc 6))) -(if #f 6 (+ (dec 3) (inc 6))) -(+ (dec 3) (inc 6)) -(+ 2 7) -(if (= 2 0) 7 (+ (dec 2) (inc 7))) -(if #f 7 (+ (dec 2) (inc 7))) -(+ (dec 2) (inc 7)) -(+ 1 8) -(if (= 1 0) 8 (+ (dec 1) (inc 8))) -(if #f 8 (+ (dec 1) (inc 8))) -(+ (dec 1) (inc 8)) -(+ 0 9) -(if (= 0 0) 9 (+ (dec 0) (inc 9))) -(if #f 9 (+ (dec 0) (inc 9))) -9 - -Iterative. - -!# + !# + )) diff --git a/sicp/tests/chapter-1/exercise-9.scm b/sicp/tests/chapter-1/exercise-9.scm @@ -0,0 +1,108 @@ +(define-library (sicp tests chapter-1 exercise-9) + (import (scheme base) + (scheme eval) + (srfi :64) + (sicp solutions chapter-1 exercise-9)) + + (begin + (test-begin "chapter-1-exercise-9") + + (for-each + (lambda (test-expr) + (test-equal (+ 4 5) + (eval test-expr + (environment '(scheme base) + '(sicp solutions chapter-1 exercise-9))))) + '((first-+ 4 5) + (if (= 4 0) + 5 + (inc (first-+ (dec 4) 5))) + (if #f + 5 + (inc (first-+ (dec 4) 5))) + (inc (first-+ (dec 4) 5)) + (inc (first-+ 3 5)) + (inc (if (= 3 0) + 5 + (inc (first-+ (dec 3) 5)))) + (inc (if #f + 5 + (inc (first-+ (dec 3) 5)))) + (inc + (inc (first-+ (dec 3) 5))) + (inc + (inc (first-+ 2 5))) + (inc + (inc (if (= 2 0) + 5 + (inc (first-+ (dec 2) 5))))) + (inc + (inc (if #f + 5 + (inc (first-+ (dec 2) 5))))) + (inc + (inc + (inc (first-+ (dec 2) 5)))) + (inc + (inc + (inc (first-+ 1 5)))) + (inc + (inc + (inc (if (= 1 0) + 5 + (inc (first-+ (dec 1) 5)))))) + (inc + (inc + (inc (if #f + 5 + (inc (first-+ (dec 1) 5)))))) + (inc (inc (inc (inc (first-+ (dec 1) 5))))) + (inc (inc (inc (inc (first-+ 0 5))))) + (inc (inc (inc (inc (if (= 0 0) 5 (inc (first-+ (dec 0) 5))))))) + (inc (inc (inc (inc (if #t 5 (inc (first-+ (dec 0) 5))))))) + (inc (inc (inc (inc 5)))) + (inc (inc (inc 6))) + (inc (inc 7)) + (inc 8) + 9)) + + ;; Recursive. + + (for-each + (lambda (test-expr) + (test-equal (+ 4 5) + (eval test-expr + (environment '(scheme base) + '(sicp solutions chapter-1 exercise-9))))) + '((second-+ 4 5) + (if (= 4 0) 5 (second-+ (dec 4) (inc 5))) + (if #f 5 (second-+ (dec 4) (inc 5))) + (second-+ 3 6) + (if (= 3 0) 6 (second-+ (dec 3) (inc 6))) + (if #f 6 (second-+ (dec 3) (inc 6))) + (second-+ (dec 3) (inc 6)) + (second-+ 2 7) + (if (= 2 0) 7 (second-+ (dec 2) (inc 7))) + (if #f 7 (second-+ (dec 2) (inc 7))) + (second-+ (dec 2) (inc 7)) + (second-+ 1 8) + (if (= 1 0) 8 (second-+ (dec 1) (inc 8))) + (if #f 8 (second-+ (dec 1) (inc 8))) + (second-+ (dec 1) (inc 8)) + (second-+ 0 9) + (if (= 0 0) 9 (second-+ (dec 0) (inc 9))) + ;; XXX: Last time I wrote this I had an error that caused an + ;; infinite loop here and the only awy I found it was by + ;; adapting it into this SRFI-64 test system. The error is + ;; that (= 0 0) is #t, but instead I evaluated it into #f, + ;; probably because I killed and yanked it all in some way or + ;; another, I think, but I am not sure. The result is that it + ;; never gotten 9. In other words: Testing is important! + #; (if #f 9 (second-+ (dec 0) (inc 9))) + ;; The right solution: + (if #t 9 (second-+ (dec 0) (inc 9))) + 9)) + + ;; Iterative. + + ))