12 | 12 |
|
13 | 13 |
-> Functionality "Run Wanda program" is implemented by
|
14 | 14 |
-> shell command "python src/wanda.py %(test-body-file)"
|
|
15 |
|
|
16 |
-> Functionality "Trace Wanda program" is implemented by
|
|
17 |
-> shell command "python src/wanda.py --trace %(test-body-file) | head -n 15"
|
15 | 18 |
|
16 | 19 |
-> Tests for functionality "Run Wanda program"
|
17 | 20 |
|
|
142 | 145 |
ten
|
143 | 146 |
===> $ ten
|
144 | 147 |
|
|
148 |
$
|
|
149 |
: $ $ ten -> $ 10 ;
|
|
150 |
ten
|
|
151 |
===> $ ten
|
|
152 |
|
|
153 |
$
|
|
154 |
: $ ten -> $ $ 10 ;
|
|
155 |
ten
|
|
156 |
===> $ ten
|
|
157 |
|
145 | 158 |
Often the `$` will appear in the leftmost position in both the pattern
|
146 | 159 |
and the replacement, as in the above examples, but this is not required.
|
147 | 160 |
|
|
149 | 162 |
---------
|
150 | 163 |
|
151 | 164 |
If we include the name of a function in its definition, recursion ought to
|
152 | |
happen. For example if we said
|
|
165 |
happen. And indeed, it does. For example if we said
|
153 | 166 |
|
154 | 167 |
: $ fact -> $ dup 1 - fact * ;
|
155 | 168 |
|
156 | 169 |
then
|
157 | 170 |
|
158 | |
4 $ fact
|
|
171 |
3 $ fact
|
159 | 172 |
|
160 | 173 |
would rewrite to
|
161 | 174 |
|
162 | |
4 $ dup 1 - fact *
|
|
175 |
3 $ dup 1 - fact *
|
163 | 176 |
|
164 | 177 |
which is fine, the next `fact` will get rewritten the same way in due course,
|
165 | 178 |
all fine except for the troublesome matter of it never terminating because we
|
166 | |
haven't given a base case.
|
|
179 |
haven't given a base case. Viewing the trace of execution for the first few
|
|
180 |
steps makes this clear:
|
|
181 |
|
|
182 |
-> Tests for functionality "Trace Wanda program"
|
|
183 |
|
|
184 |
3 $
|
|
185 |
: $ fact -> $ dup 1 - fact * ;
|
|
186 |
fact
|
|
187 |
===> 3 $ fact
|
|
188 |
===> 3 $ dup 1 - fact *
|
|
189 |
===> 3 3 $ 1 - fact *
|
|
190 |
===> 3 3 1 $ - fact *
|
|
191 |
===> 3 2 $ fact *
|
|
192 |
===> 3 2 $ dup 1 - fact * *
|
|
193 |
===> 3 2 2 $ 1 - fact * *
|
|
194 |
===> 3 2 2 1 $ - fact * *
|
|
195 |
===> 3 2 1 $ fact * *
|
|
196 |
===> 3 2 1 $ dup 1 - fact * * *
|
|
197 |
===> 3 2 1 1 $ 1 - fact * * *
|
|
198 |
===> 3 2 1 1 1 $ - fact * * *
|
|
199 |
===> 3 2 1 0 $ fact * * *
|
|
200 |
===> 3 2 1 0 $ dup 1 - fact * * * *
|
|
201 |
===> 3 2 1 0 0 $ 1 - fact * * * *
|
|
202 |
|
|
203 |
-> Tests for functionality "Run Wanda program"
|
167 | 204 |
|
168 | 205 |
What would be great would be some way for `0 fact` to be immediately rewritten
|
169 | 206 |
into `1` instead of recursing.
|