Olin Shivers <shivers <at> clark.lcs.mit.edu>
1995-04-19 16:43:08 GMT
table-walk is not the right primitive, or at least should be augmented
(table-reduce proc seed table) would allow side-effect free ways to
loop over a table, computing some value. table->list, max-elt, etc., etc.
are all defineable with this primitive (as is table-walk).
Generally speaking, I feel that iteration patterns are best primitively
expressed in two ways:
- a reducer
- a generator
Generators are nice because they let you integrate the iteration pattern
in with other iteration patterns -- i.e. if you want a loop that
- loops x over the elts of a hash table
- y over the elts of another hash table
- z over a vector
then having generators for all three allows you to merge them into a common
loop. Single-data structure reducers don't allow this.
A general generator interface might be
(table-cursor table) ; A cursor captures iteration state.
(eos-cursor? cursor table) ; At end of sequence?
(step-cursor cursor table) -> [cursor' elt] or error if at end of iteration
For a vector, the cursor is an integer index.
One could argue about whether or not the cursor should contain the
object into which it points -- flushing the TABLE arg from the CURSOR-DONE?
and STEP-CURSOR procs.
As a further sleaze, one could hardwire the end-of-sequence cursor to be #f,