learning-sicp

My embarrassing half assed SICP run.
git clone https://kaka.farm/~git/learning-sicp
Log | Files | Refs

exercise-9.scm (3273B)


      1 (define-library (sicp tests chapter-1 exercise-9)
      2   (import (scheme base)
      3           (scheme eval)
      4           (srfi :64)
      5           (sicp solutions chapter-1 exercise-9))
      6 
      7   (begin
      8     (test-begin "chapter-1-exercise-9")
      9 
     10     (for-each
     11      (lambda (test-expr)
     12        (test-equal (+ 4 5)
     13          (eval test-expr
     14                (environment '(scheme base)
     15                             '(sicp solutions chapter-1 exercise-9)))))
     16      '((first-+ 4 5)
     17        (if (= 4 0)
     18            5
     19            (inc (first-+ (dec 4) 5)))
     20        (if #f
     21            5
     22            (inc (first-+ (dec 4) 5)))
     23        (inc (first-+ (dec 4) 5))
     24        (inc (first-+ 3 5))
     25        (inc (if (= 3 0)
     26                 5
     27                 (inc (first-+ (dec 3) 5))))
     28        (inc (if #f
     29                 5
     30                 (inc (first-+ (dec 3) 5))))
     31        (inc
     32         (inc (first-+ (dec 3) 5)))
     33        (inc
     34         (inc (first-+ 2 5)))
     35        (inc
     36         (inc (if (= 2 0)
     37                  5
     38                  (inc (first-+ (dec 2) 5)))))
     39        (inc
     40         (inc (if #f
     41                  5
     42                  (inc (first-+ (dec 2) 5)))))
     43        (inc
     44         (inc
     45          (inc (first-+ (dec 2) 5))))
     46        (inc
     47         (inc
     48          (inc (first-+ 1 5))))
     49        (inc
     50         (inc
     51          (inc (if (= 1 0)
     52                   5
     53                   (inc (first-+ (dec 1) 5))))))
     54        (inc
     55         (inc
     56          (inc (if #f
     57                   5
     58                   (inc (first-+ (dec 1) 5))))))
     59        (inc (inc (inc (inc (first-+ (dec 1) 5)))))
     60        (inc (inc (inc (inc (first-+ 0 5)))))
     61        (inc (inc (inc (inc (if (= 0 0) 5 (inc (first-+ (dec 0) 5)))))))
     62        (inc (inc (inc (inc (if #t 5 (inc (first-+ (dec 0) 5)))))))
     63        (inc (inc (inc (inc 5))))
     64        (inc (inc (inc 6)))
     65        (inc (inc 7))
     66        (inc 8)
     67        9))
     68 
     69     ;; Recursive.
     70 
     71     (for-each
     72      (lambda (test-expr)
     73        (test-equal (+ 4 5)
     74          (eval test-expr
     75                (environment '(scheme base)
     76                             '(sicp solutions chapter-1 exercise-9)))))
     77      '((second-+ 4 5)
     78        (if (= 4 0) 5 (second-+ (dec 4) (inc 5)))
     79        (if #f 5 (second-+ (dec 4) (inc 5)))
     80        (second-+ 3 6)
     81        (if (= 3 0) 6 (second-+ (dec 3) (inc 6)))
     82        (if #f 6 (second-+ (dec 3) (inc 6)))
     83        (second-+ (dec 3) (inc 6))
     84        (second-+ 2 7)
     85        (if (= 2 0) 7 (second-+ (dec 2) (inc 7)))
     86        (if #f 7 (second-+ (dec 2) (inc 7)))
     87        (second-+ (dec 2) (inc 7))
     88        (second-+ 1 8)
     89        (if (= 1 0) 8 (second-+ (dec 1) (inc 8)))
     90        (if #f 8 (second-+ (dec 1) (inc 8)))
     91        (second-+ (dec 1) (inc 8))
     92        (second-+ 0 9)
     93        (if (= 0 0) 9 (second-+ (dec 0) (inc 9)))
     94        ;; XXX: Last time I wrote this I had an error that caused an
     95        ;; infinite loop here and the only awy I found it was by
     96        ;; adapting it into this SRFI-64 test system.  The error is
     97        ;; that (= 0 0) is #t, but instead I evaluated it into #f,
     98        ;; probably because I killed and yanked it all in some way or
     99        ;; another, I think, but I am not sure.  The result is that it
    100        ;; never gotten 9.  In other words: Testing is important!
    101        #; (if #f 9 (second-+ (dec 0) (inc 9)))
    102        ;; The right solution:
    103        (if #t 9 (second-+ (dec 0) (inc 9)))
    104        9))
    105 
    106     ;; Iterative.
    107 
    108     ))