learning-sicp

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

exercise-21.scm (1673B)


      1 (define-library (sicp tests chapter-1 exercise-21)
      2   (import
      3    (scheme base)
      4    (srfi :1)
      5    (srfi :26)
      6    (srfi :64)
      7    (only (guile) random)
      8    (sicp solutions chapter-1 exercise-21)
      9    )
     10 
     11   (begin
     12     (test-begin "chapter-1-exercise-21")
     13     (test-group "prime"
     14       (test-equal '(#t #f #t #f #t #f) (map (cut divides? 2 <>) '(2 3 4 5 6 7)))
     15       (test-equal '(2 3 2 5 2 7) (map smallest-divisor '(2 3 4 5 6 7)))
     16       (test-equal '(#t #t #f #t #f #t) (map prime? '(2 3 4 5 6 7))))
     17 
     18     (test-group "expmod"
     19       (let ((cases (map (lambda (_) (list (+ 1 (random 100))
     20                                           (+ 1 (random 100))))
     21                         (iota 20))))
     22         (test-equal
     23             (map (lambda (x) (apply (lambda (a n) (= (expmod a
     24                                                              n
     25                                                              n)
     26                                                      a))
     27                                     x)) cases)
     28           (map (lambda (x) (apply (lambda (a n) (= (remainder (expt a n)
     29                                                               n)
     30                                                    a))
     31                                   x))
     32                cases))))
     33 
     34     (test-group "fast-prime"
     35       (test-equal
     36           (map (lambda (n) (fast-prime? n n)) (iota 100 2))
     37         (map (cut prime? <>) (iota 100 2))))
     38 
     39     ;; XXX
     40 
     41 
     42     (test-group "chapter-1-exercise-21"
     43       (let ((cases '(199 1999 19999))
     44             (results '(199 1999 7)))
     45         (test-equal
     46             results
     47           (map (lambda (case) (smallest-divisor case)) cases))))
     48     (test-end "chapter-1-exercise-21")))