exercise-20.scm (7428B)
1 2 ;; Exercise 1.20 3 4 5 6 (define (gcd-iterative a b) 7 ;; a = b*d + r 8 (if (= b 0) 9 a 10 (gcd-iterative b (remainder a b)))) 11 12 ;; Normal-order 13 14 (test-begin "1.20") 15 (test-equal (gcd-iterative 206 40) (gcd 206 40)) 16 (test-equal (if (= 40 0) 17 206 18 (gcd 40 (remainder 206 40))) 2) 19 (test-equal (if #f 20 206 21 (gcd 40 (remainder 206 40))) 2) 22 (test-equal (gcd 40 (remainder 206 40)) 2) 23 (test-equal (if (= (remainder 206 40) 0) ;; 1 remainder 24 40 25 (gcd (remainder 206 40) 26 (remainder 40 27 (remainder 206 40)))) 2) 28 (test-equal (if (= 6 0) 29 40 30 (gcd (remainder 206 40) 31 (remainder 40 32 (remainder 206 40)))) 2) 33 (test-equal (gcd (remainder 206 40) 34 (remainder 40 35 (remainder 206 40))) 2) 36 (test-equal (if (= (remainder 40 37 (remainder 206 40)) 0) 38 (remainder 206 40) 39 (gcd (remainder 40 40 (remainder 206 40)) 41 (remainder (remainder 206 40) 42 (remainder 40 43 (remainder 206 40))))) 2) 44 (test-equal (if (= 4 0) ;; previous 1 and 2 here equals 3 45 (remainder 206 40) 46 (gcd (remainder 40 47 (remainder 206 40)) 48 (remainder (remainder 206 40) 49 (remainder 40 50 (remainder 206 40))))) 2) 51 (test-equal (gcd (remainder 40 52 (remainder 206 40)) 53 (remainder (remainder 206 40) 54 (remainder 40 55 (remainder 206 40)))) 2) 56 (test-equal 2 (if (= (remainder (remainder 206 40) 57 (remainder 40 58 (remainder 206 40))) 0) 59 (remainder 40 60 (remainder 206 40)) 61 (gcd (remainder (remainder 206 40) 62 (remainder 40 63 (remainder 206 40))) 64 (remainder (remainder 40 65 (remainder 206 40)) 66 (remainder (remainder 206 40) 67 (remainder 40 68 (remainder 206 40))))))) 69 (test-equal 2 (if (= 2 0) ;; 4 now plus 3 previously is 5 70 (remainder 40 71 (remainder 206 40)) 72 (gcd (remainder (remainder 206 40) 73 (remainder 40 74 (remainder 206 40))) 75 (remainder (remainder 40 76 (remainder 206 40)) 77 (remainder (remainder 206 40) 78 (remainder 40 79 (remainder 206 40))))))) 80 (test-equal 2 (gcd (remainder (remainder 206 40) 81 (remainder 40 82 (remainder 206 40))) 83 (remainder (remainder 40 84 (remainder 206 40)) 85 (remainder (remainder 206 40) 86 (remainder 40 87 (remainder 206 40)))))) 88 (test-equal 2 (if (= (remainder (remainder 40 89 (remainder 206 40)) 90 (remainder (remainder 206 40) 91 (remainder 40 92 (remainder 206 40)))) 0) 93 (remainder (remainder 206 40) 94 (remainder 40 95 (remainder 206 40))) 96 (gcd 97 (remainder (remainder 40 98 (remainder 206 40)) 99 (remainder (remainder 206 40) 100 (remainder 40 101 (remainder 206 40)))) 102 (reminder (remainder (remainder 206 40) 103 (remainder 40 104 (remainder 206 40))) 105 (remainder (remainder 40 106 (remainder 206 40)) 107 (remainder (remainder 206 40) 108 (remainder 40 109 (remainder 206 40)))))))) 110 (test-equal 2 (if (= 0 0) ;; 5 previously plus 7 = 12 111 (remainder (remainder 206 40) 112 (remainder 40 113 (remainder 206 40))) 114 (gcd 115 (remainder (remainder 40 116 (remainder 206 40)) 117 (remainder (remainder 206 40) 118 (remainder 40 119 (remainder 206 40)))) 120 (reminder (remainder (remainder 206 40) 121 (remainder 40 122 (remainder 206 40))) 123 (remainder (remainder 40 124 (remainder 206 40)) 125 (remainder (remainder 206 40) 126 (remainder 40 127 (remainder 206 40)))))))) 128 (test-equal 2 (remainder (remainder 206 40) 129 (remainder 40 130 (remainder 206 40)))) ;; 12 previously and 4 now is 16. 131 132 ;; Now for applicative-order evaluation: 133 (test-equal 2 (gcd 206 40)) 134 (test-equal 2 (if (= 40 0) 135 206 136 (gcd 40 (remainder 206 40)))) 137 (test-equal 2 (if #f 138 206 139 (gcd 40 (remainder 206 40)))) 140 (test-equal 2 (gcd 40 (remainder 206 40))) 141 (test-equal 2 (gcd 40 6)) ;; 1 142 (test-equal 2 (if (= 6 0) 143 40 144 (gcd 6 (remainder 40 6)))) 145 (test-equal 2 (gcd 6 (remainder 40 6))) 146 (test-equal 2 (gcd 6 4)) ;; 2 147 (test-equal 2 (if (= 4 0) 148 6 149 (gcd 4 (remainder 6 4)))) 150 (test-equal 2 (if #f 151 6 152 (gcd 4 (remainder 6 4)))) 153 (test-equal 2 (gcd 4 (remainder 6 4))) 154 (test-equal 2 (gcd 4 2)) ;; 3 155 (test-equal 2 (if (= 2 0) 156 4 157 (gcd 2 (remainder 4 2)))) 158 (test-equal 2 (if #f 159 4 160 (gcd 2 (remainder 4 2)))) 161 (test-equal 2 (gcd 2 (remainder 4 2))) 162 (test-equal 2 (gcd 2 0)) ;; 4 163 (test-equal 2 (if (= 0 0) 164 2 165 (gcd 0 (remainder 2 0)))) 166 (test-equal 2 (if #t 167 2 168 (gcd 0 (remainder 2 0)))) 169 (test-equal 2 2) 170 (test-end "1.20")