learning-sicp

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

commit b32210566cbde0ad630e39f24ad0b068c8e0231f
parent 77d3ca3eae65eea2e77be66e6cbf41aadeddd165
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Sun, 19 Nov 2023 13:13:12 +0200

Change how Makefile builds the targets and stuff.  Add a bunch of exercises.  Add some stuff to the statistics script.

Diffstat:
MMakefile | 21+++++++++++++++------
Msicp/solutions/chapter-1/exercise-2.scm | 25+++++++++++--------------
Msicp/solutions/chapter-1/exercise-4.scm | 29++++++++++++++---------------
Msicp/solutions/chapter-1/exercise-7.scm | 7+++----
Msicp/statistics.scm | 49+++++++++++++++++++++++++++++++------------------
Asicp/tests/chapter-1/exercise-2.scm | 12++++++++++++
Asicp/tests/chapter-1/exercise-4.scm | 35+++++++++++++++++++++++++++++++++++
Asicp/tests/chapter-1/exercise-5.scm | 8++++++++
Asicp/tests/chapter-1/exercise-6.scm | 4++++
Asicp/tests/chapter-1/exercise-7.scm | 16++++++++++++++++
10 files changed, 149 insertions(+), 57 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,17 +1,26 @@ -tests = $(wildcard sicp/tests/chapter-*/exercise-*.scm) -tests_results = $(foreach test,$(tests),$(shell dirname $(test) | sed 's#sicp/tests/##')-$(shell basename -s ".scm" $(test)).log) +#tests = $(wildcard sicp/tests/chapter-*/exercise-*.scm) +chapter_numbers := 1 2 3 4 5 +exercises_per_chapter := 46 97 82 79 52 +solutions := $(foreach chapter,$(chapter_numbers),$(foreach exercise,$(shell seq 1 $(word $(chapter),$(exercises_per_chapter))),sicp/solutions/chapter-$(chapter)/exercise-$(exercise).scm)) +tests := $(foreach chapter,$(chapter_numbers),$(foreach exercise,$(shell seq 1 $(word $(chapter),$(exercises_per_chapter))),sicp/tests/chapter-$(chapter)/exercise-$(exercise).scm)) +tests_logs := $(foreach chapter,$(chapter_numbers),$(foreach exercise,$(shell seq 1 $(word $(chapter),$(exercises_per_chapter))),chapter-$(chapter)-exercise-$(exercise).log)) .PHONY: all -all: $(tests_results) +all: $(tests_logs) .PHONY: print_variables print_variables: + printf "%s\n" $(solutions) printf "%s\n" $(tests) - printf "%s\n" $(tests_results) + printf "%s\n" $(tests_logs) -$(tests_results): $(tests) +$(tests_logs): guile -L . `printf $@ | sed -E 's#(chapter-[0-9]*)-(exercise-[0-9]+).log#sicp/tests/\1/\2.scm#g'` .PHONY: statistics statistics: - guile -L . sicp/statistics.scm + guile -L . -e '(@ (sicp statistics) main)' -s sicp/statistics.scm + +.PHONY: statistics +tests-statistics: statistics.log + guile -L . -s sicp/statistics-tests.scm diff --git a/sicp/solutions/chapter-1/exercise-2.scm b/sicp/solutions/chapter-1/exercise-2.scm @@ -1,6 +1,6 @@ (define-library (sicp solutions chapter-1 exercise-2) (import (scheme base)) - (import (srfi srfi-64)) + (export expression) (begin @@ -15,16 +15,13 @@ !# - (test-begin "1.2") - (test-equal - (/ (+ 5 - 4 - (- 2 - (- 3 - (+ 6 - (/ 4 5))))) - (* 3 - (- 6 2) - (- 2 7))) - (/ (- 37) 150)) - (test-end "1.2"))) + (define expression + (/ (+ 5 + 4 + (- 2 + (- 3 + (+ 6 + (/ 4 5))))) + (* 3 + (- 6 2) + (- 2 7)))))) diff --git a/sicp/solutions/chapter-1/exercise-4.scm b/sicp/solutions/chapter-1/exercise-4.scm @@ -1,7 +1,9 @@ (define-library (sicp solutions chapter-1 exercise-4) (import (scheme base)) (import (srfi srfi-64)) - + (export + a-plus-abs-b + a-plus-abs-b-2) (begin (define (a-plus-abs-b a b) @@ -10,18 +12,15 @@ ;; The result of the "if" expression will be either the procedure "+" or ;; the procedure "-", which is then applied to "a" and "b". + ;; a-plus-abs-b, when b > 0, is (+ a b), which is equivalent to (+ + ;; a (+ b)) + ;; + ;; when b = 0, (- a 0), which is equivalent to (+ a 0) + ;; + ;; and a when b <= 0, (- a b), equivalent to (+ a (- b)) + + ;; Considering the definition of the absolute function, we could + ;; replace a-plus-abs-b with: - (test-begin "1.4") - (test-equal - (+ 2 3) - (a-plus-abs-b 2 3)) - (test-equal - (+ -2 3) - (a-plus-abs-b -2 3)) - (test-equal - (- 2 -3) - (a-plus-abs-b 2 -3)) - (test-equal - (- -2 -3) - (a-plus-abs-b -2 -3)) - (test-end "1.4"))) + (define (a-plus-abs-b-2 a b) + (+ a (abs b))))) diff --git a/sicp/solutions/chapter-1/exercise-7.scm b/sicp/solutions/chapter-1/exercise-7.scm @@ -1,6 +1,7 @@ (define-library (sicp solutions chapter-1 exercise-7) (import (scheme base)) (import (scheme write)) + (export square-root) (begin #! @@ -19,6 +20,7 @@ !# + ;; TODO: The wordy parts. (define (square-root x) (define (average a b) @@ -48,7 +50,4 @@ (square-root-iter new-guess x))) - (square-root-iter 1 x)) - - (write (square-root 0.000000000001)) (newline) - (write (square-root (/ 1 0.000000000001))) (newline))) + (square-root-iter 1 x)))) diff --git a/sicp/statistics.scm b/sicp/statistics.scm @@ -9,6 +9,10 @@ (ice-9 pretty-print) (ice-9 regex) (ice-9 textual-ports)) + (export + make-ranges + main) + (begin (define number-of-exercises '(46 97 82 79 52)) @@ -36,19 +40,27 @@ (let ((test-filename (make-test-filename chapter-number exercise-number))) (not (file-exists? test-filename)))) + (define (grep-q filename term) + (if (file-exists? filename) + (let ((retval (system* "grep" "-q" "XXX" filename))) + (zero? retval)) + #f)) + (define (failing-test-log? chapter-number exercise-number) - (let ((test-log-filename (make-test-log-filename chapter-number exercise-number))) - (if (file-exists? test-log-filename) - (let ((retval (system* "grep" "-q" "fail" test-log-filename))) - (zero? retval)) - #f))) + (grep-q (make-test-log-filename chapter-number exercise-number) + "fail")) + + (define (XXX? chapter-number exercise-number) + (grep-q (make-solution-filename chapter-number exercise-number) + "XXX")) + + (define (TODO? chapter-number exercise-number) + (grep-q (make-test-log-filename chapter-number exercise-number) + "TODO")) (define (erroring-test-log? chapter-number exercise-number) - (let ((test-log-filename (make-test-log-filename chapter-number exercise-number))) - (if (file-exists? test-log-filename) - (let ((retval (system* "grep" "-q" "error" test-log-filename))) - (zero? retval)) - #f))) + (grep-q (make-test-log-filename chapter-number exercise-number) + "error")) (define (make-range a b) (list (min a b) @@ -113,11 +125,12 @@ (if (predicate? chapter-number exercise-number)) exercise-number)))))) - (print-ranges missing-solution? - "Missing solutions files of chapter ~A:~%") - (print-ranges missing-tests? - "Missing tests files of chapter ~A:~%") - (print-ranges failing-test-log? - "Failing test logs of chapter ~A:~%") - (print-ranges erroring-test-log? - "Errors in test logs of chapter ~A:~%"))) + (define (main args) + (print-ranges missing-solution? + "Missing solutions files of chapter ~A:~%") + (print-ranges missing-tests? + "Missing tests files of chapter ~A:~%") + (print-ranges failing-test-log? + "Failing test logs of chapter ~A:~%") + (print-ranges erroring-test-log? + "Errors in test logs of chapter ~A:~%")))) diff --git a/sicp/tests/chapter-1/exercise-2.scm b/sicp/tests/chapter-1/exercise-2.scm @@ -0,0 +1,12 @@ +(define-library (sicp tests chapter-1 exercise-2) + (import (scheme base) + (srfi :64) + (sicp solutions chapter-1 exercise-2)) + + (begin + (test-begin "chapter-1-exercise-2") + (test-equal + expression + (/ (- 37) 150)) + #; (test-end "1.2") + )) diff --git a/sicp/tests/chapter-1/exercise-4.scm b/sicp/tests/chapter-1/exercise-4.scm @@ -0,0 +1,35 @@ +(define-library (sicp tests chapter-1 exercise-4) + (import (scheme base) + (srfi :1) + (srfi :64) + (sicp solutions chapter-1 exercise-4)) + + (begin + (test-begin "chapter-1-exercise-4") + (test-equal + (+ 2 3) + (a-plus-abs-b 2 3)) + (test-equal + (+ -2 3) + (a-plus-abs-b -2 3)) + (test-equal + (- 2 -3) + (a-plus-abs-b 2 -3)) + (test-equal + (- -2 -3) + (a-plus-abs-b -2 -3)) + + (for-each + (lambda (a) + (for-each (lambda (b) + (test-equal + (a-plus-abs-b + a + b + ) + (a-plus-abs-b-2 + a + b + ))) + (iota 10 -5))) + (iota 10 -5)))) diff --git a/sicp/tests/chapter-1/exercise-5.scm b/sicp/tests/chapter-1/exercise-5.scm @@ -0,0 +1,8 @@ +(define-library (sicp tests chapter-1 exercise-5) + (import (scheme base) + (srfi :64)) + + (begin + (test-begin "chapter-1-exercise-5") + ;; Non-programmatic exercise. + )) diff --git a/sicp/tests/chapter-1/exercise-6.scm b/sicp/tests/chapter-1/exercise-6.scm @@ -0,0 +1,4 @@ +(import (srfi :64)) + +(test-begin "chapter-1-exercise-6") +;; Non-programmatic exercise. diff --git a/sicp/tests/chapter-1/exercise-7.scm b/sicp/tests/chapter-1/exercise-7.scm @@ -0,0 +1,16 @@ +(define-library (sicp tests chapter-1 exercise-7) + (import (scheme base) + (scheme inexact) + (srfi :64) + (sicp solutions chapter-1 exercise-7)) + + (begin + (test-begin "chapter-1-exercise-7") + (test-approximate + (sqrt 4.0) + (square-root 4.0) + 0.0001) + (test-approximate + (sqrt 9.0) + (square-root 9.0) + 0.0001)))