exercise-16.scm (1322B)
1 (define-library (sicp tests chapter-1 exercise-16) 2 (import (scheme base) 3 (srfi :64) 4 (sicp solutions chapter-1 exercise-16)) 5 6 (begin 7 (test-begin "chapter-1-exercise-16") 8 9 (test-group "flatten" 10 ;; Why...? 11 (test-equal 12 '(1 2 3 4 5 6) 13 (flatten '((1 2) (3 4) (5 6))))) 14 15 (test-group "cartesian" 16 ;; Why...? 17 (test-equal 18 '((1 . 4) 19 (1 . 5) 20 (1 . 6) 21 (2 . 4) 22 (2 . 5) 23 (2 . 6) 24 (3 . 4) 25 (3 . 5) 26 (3 . 6)) 27 (cartesian '(1 2 3) 28 '(4 5 6)))) 29 30 (test-group "fast-expt-recursive-versus-fast-expt-iterative" 31 (test-equal (fast-expt-iterative 2 3) (fast-expt-recursive 2 3)) 32 ;; 1 * 2**3 = 33 ;; 2 * 2**2 = 2 * (2**1)**2 34 ;; 2 * 35 (test-equal (fast-expt-iterative 3 5) (fast-expt-recursive 3 5)) 36 ;; a * b^n = 37 ;; 1 * 3^5 = 38 ;; 3 * 3^4 = 39 ;; 3 * 3 * 3 * 3^2 40 ;; 3 * 3 * 3 * 3 * 3 41 (test-equal (fast-expt-iterative 5 7) (fast-expt-recursive 5 7)) 42 ;; 5**7 = 43 ;; 5 * (5**3)**2 = 44 ;; 5 * (5**2)**3 45 ;; 5 * 5**2 * 5**3 = 46 ;; 5 * 5**2 * 5 * 5**2 = 47 (test-equal (fast-expt-iterative 7 11) (fast-expt-recursive 7 11))) 48 49 (test-end "chapter-1-exercise-16")))