To generate the next-states, what is wrong with using
for choice in available do
delete(choice, available) -> remainder;
....
etc.?
This iterative construct doesn't use the pattern matcher, but is fairly
easy to explain, and (I would have thought) your students ought already
to understand it. If you want, you can explain how to write delete:
define mydelete(item, list) -> remainder;
vars before, after;
if list matches [??before ^item ??after] then
[^^before ^^after] -> remainder;
else
list -> remainder;
endif;
enddefine;
This is reasonably efficient. You can also in-line it, in this context,
since you know the match will succeed:
for choice in available do
available --> [??before ^choice ??after];
[^^before ^^after] -> remainder;
....
I think that trying to do the iteration with matching just confuses the
real issue (search) in this contetxt. By all means teach how to do
iteration using matching, but then treat iteration as a primitive
operation. That's why it is there. I slightly prefer the version using
delete for this reason, too. It helps to show students that a big,
complicated program can be understood as really a small, simple program
which is made out of other, equally small, simple programs. Lots of
times (ie recursively). And when they are *really* grown up, they will
be able to write programs thousands of lines long. One line at a time :-).
Cheers,
Jonathan
|