exercise-14.scm (6137B)
1 (define-library (sicp tests chapter-3 exercise-14) 2 (import (scheme base)) 3 (import (srfi :64)) 4 5 (begin 6 (test-begin "chapter-3-exercise-14") 7 8 (define v '(a b c d)) 9 10 ;; v: [a|-]-> [b|-]-> [c|-]-> [d|()] 11 12 ;; iteration 1 13 (define y '()) (define x v) 14 ;; v: [a|-]-> [b|-]-> [c|-]-> [d|()] 15 ;; ^ 16 ;; | 17 ;; x: --+ 18 ;; y: () 19 (test-equal '(a b c d) x) 20 (test-equal '() y) 21 22 (define temp (cdr x)) 23 ;; v: [a|-]-> [b|-]-> [c|-]-> [d|()] 24 ;; ^ ^ 25 ;; | | 26 ;; x: --+ | 27 ;; y: () | 28 ;; temp: -------+ 29 (test-equal '(b c d) temp) 30 31 (set-cdr! x y) 32 ;; v: [a|()] [b|-]-> [c|-]-> [d|()] 33 ;; ^ ^ 34 ;; | | 35 ;; x: --+ | 36 ;; y: () | 37 ;; temp: -------+ 38 (test-equal '(a) x) 39 40 ;; iteration 2 41 (define y x) (define x temp) 42 ;; v: [a|()] [b|-]-> [c|-]-> [d|()] 43 ;; ^ ^ 44 ;; | | 45 ;; +-+ | 46 ;; | | 47 ;; +------+ 48 ;; | | | 49 ;; x: --+ | | 50 ;; y: ----+ | 51 ;; temp: ------+ 52 (test-equal '(b c d) x) 53 (test-equal '(a) y) 54 55 (define temp (cdr x)) 56 ;; v: [a|()] [b|-]-> [c|-]-> [d|()] 57 ;; ^ ^ ^ 58 ;; | | | 59 ;; +-+ | | 60 ;; | | | 61 ;; +------+ | 62 ;; | | | 63 ;; x: --+ | | 64 ;; y: ----+ | 65 ;; temp: --------------+ 66 (test-equal '(c d) temp) 67 68 (set-cdr! x y) 69 ;; +----------+ 70 ;; | | 71 ;; V | 72 ;; v: [a|()] [b|-]-+ [c|-]-> [d|()] 73 ;; ^ ^ ^ 74 ;; | | | 75 ;; +-+ | | 76 ;; | | | 77 ;; +------+ | 78 ;; | | | 79 ;; x: --+ | | 80 ;; y: ----+ | 81 ;; temp: --------------+ 82 (test-equal '(b a) x) 83 84 ;; iteration 3 85 (define y x) (define x temp) 86 ;; +----------+ 87 ;; | | 88 ;; V | 89 ;; v: [a|()] [b|-]-+ [c|-]-> [d|()] 90 ;; ^ ^ 91 ;; | | 92 ;; +--------------+ 93 ;; | | | 94 ;; | +----+ | 95 ;; | | | 96 ;; x: --+ | | 97 ;; y: ----+ | 98 ;; temp: --------------+ 99 (test-equal '(c d) x) 100 (test-equal '(b a) y) 101 102 (define temp (cdr x)) 103 ;; +----------+ 104 ;; | | 105 ;; V | 106 ;; v: [a|()] [b|-]-+ [c|-]-> [d|()] 107 ;; ^ ^ ^ 108 ;; | | | 109 ;; +--------------+ | 110 ;; | | | 111 ;; | +----+ | 112 ;; | | | 113 ;; x: --+ | | 114 ;; y: ----+ | 115 ;; temp: ----------------------+ 116 (test-equal '(d) temp) 117 118 (set-cdr! x y) 119 ;; +-----------+ 120 ;; | | 121 ;; +----------+ | 122 ;; | | | | 123 ;; V V | | 124 ;; v: [a|()] [b|-]-+ [c|-]-+ [d|()] 125 ;; ^ ^ ^ 126 ;; | | | 127 ;; +--------------+ | 128 ;; | | | 129 ;; | +----+ | 130 ;; | | | 131 ;; x: --+ | | 132 ;; y: ----+ | 133 ;; temp: ----------------------+ 134 (test-equal '(c b a) x) 135 136 ;; iteration 4 137 (define y x) (define x temp) 138 ;; +-----------+ 139 ;; | | 140 ;; +----------+ | 141 ;; | | | | 142 ;; V V | | 143 ;; v: [a|()] [b|-]-+ [c|-]-+ [d|()] 144 ;; ^ ^ 145 ;; | | 146 ;; +------------+ | 147 ;; | | 148 ;; +----------------------+ 149 ;; | | | 150 ;; x: --+ | | 151 ;; y: ----+ | 152 ;; temp: ----------------------+ 153 (test-equal '(d) x) 154 (test-equal '(c b a) y) 155 156 (define temp (cdr x)) 157 ;; +-----------+ 158 ;; | | 159 ;; +----------+ | 160 ;; | | | | 161 ;; V V | | 162 ;; v: [a|()] [b|-]-+ [c|-]-+ [d|()] 163 ;; ^ ^ ^ 164 ;; | | | 165 ;; +------------+ | | 166 ;; | | | 167 ;; +----------------------+ | 168 ;; | | | 169 ;; x: --+ | | 170 ;; y: ----+ | 171 ;; temp: ------------------------+ 172 (test-equal '() temp) 173 174 (set-cdr! x y) 175 ;; +-----------+ 176 ;; | | 177 ;; +----------+ +-----------+ 178 ;; | | | | | | 179 ;; V V | V | | 180 ;; v: [a|()] [b|-]-+ [c|-]-+ [d|-]-+ 181 ;; ^ ^ 182 ;; | | () 183 ;; +------------+ | ^ 184 ;; | | | 185 ;; +----------------------+ | 186 ;; | | | 187 ;; x: --+ | | 188 ;; y: ----+ | 189 ;; temp: ------------------------+ 190 (test-equal '(d c b a) x) 191 192 ;; iteration 5 193 (define y x) (define x temp) 194 ;; +-----------+ 195 ;; | | 196 ;; +----------+ +-----------+ 197 ;; | | | | | | 198 ;; V V | V | | 199 ;; v: [a|()] [b|-]-+ [c|-]-+ [d|-]-+ 200 ;; ^ 201 ;; | () 202 ;; | ^ 203 ;; | | 204 ;; +--------------------+ | 205 ;; | | 206 ;; x: ----|----------------------+ 207 ;; y: ----+ | 208 ;; temp: ------------------------+ 209 (test-equal '() x) 210 (test-equal '(d c b a) y) 211 (test-equal '(a) v) 212 (test-end "chapter-3-exercise-14")))