learning-sicp

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

commit 43d755157b2d40909a0feac5f53d62b93789ee1f
parent 46989fdf7512d7d702ac20fc3b5643668052f2ab
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Sun, 16 Apr 2023 13:19:39 +0300

Add solution to exercise 3.16.

Diffstat:
Asicp/tests/3_16.scm | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 83 insertions(+), 0 deletions(-)

diff --git a/sicp/tests/3_16.scm b/sicp/tests/3_16.scm @@ -0,0 +1,83 @@ +(define-library (sicp tests 3_16) + (import (scheme base)) + (import (srfi :64)) + + (begin + (test-begin "3.16") + + (define (count-pairs x) + (if (not (pair? x)) + 0 + (+ (count-pairs (car x)) + (count-pairs (cdr x)) + 1))) + + (define three-pairs '(1 2 3)) + + (define four-pairs + (let ([a (list 1 2)] + [b (list 3)]) + (set-car! (cdr a) b) + (set-cdr! (cdr a) b) + a)) + + (define seven-pairs + (let ([a (list 1)] + [b (list 2)] + [c (list 3)]) + (set-car! a b) + (set-cdr! a b) + (set-car! b c) + (set-cdr! b c) + a)) + + ;; +---+---+ +---+---+ +---+---+ + ;; three-pairs: | 1 | *-+-->| 2 | *-+-->| 3 | * | + ;; +---+---+ +---+---+ +---+---+ + + ;; +---+---+ +---+---+ +---+---+ + ;; four-pairs: | 1 | *-+-->| * | *-+-->| 3 | * | + ;; +---+---+ +-+-+---+ +---+---+ + ;; | ^ + ;; | | + ;; +-------------+ + + ;; +---+---+ +---+---+ +---+---+ + ;; seven-pairs: | * | *-+-->| * | *-+-->| 3 | * | + ;; +-+-+---+ +-+-+---+ +---+---+ + ;; | | ^ ^ + ;; | | | | + ;; +-----------|-+ | + ;; | | + ;; +-------------+ + + (test-equal + '(1 2 3) + three-pairs) + + (test-equal + (cons 1 + (cons (cons 3 '()) + (cons 3 '()))) + four-pairs) + + (test-equal + (cons (cons (cons 3 '()) + (cons 3 '())) + (cons (cons 3 '()) + (cons 3 '()))) + seven-pairs) + + (test-equal + 3 + (count-pairs three-pairs)) + + (test-equal + 4 + (count-pairs four-pairs)) + + (test-equal + 7 + (count-pairs seven-pairs)) + + (test-end "3.16")))