Suggestions for DOLIST

Started by Jeremy Dunn, January 12, 2008, 02:59:53 PM

Previous topic - Next topic

Jeremy Dunn

In thinking about the DOLIST command I had some ideas that might be useful. The protected symbol $idx stores the current loop index. I would suggest having two more protected symbols that would be called $length and $last. $length stores the length of the list and $last stores the last index or $length - 1. This way if I want to find out if I'm on the last index I can just write (= $idx $last).



Another possible change would be adding to the general structure. The current structure is



(dolist (sym list break) body)



I would like this changed to



(dolist (sym list break body-1st body-last body-nth) body)



This would would work as follows: The extra bodies can either be single expressions or groups of statements thrown together using BEGIN. On the 1st element of the loop the statements in body-1st are executed, on the last element of the loop the statements in body-last are excecuted and for all other elements body-nth is executed. Any operations that occur for all elements are exectued in body as normal.



Now if we have the form



(dolist (sym list break body-1st/last body-nth) body)



where we only have two bodies then the 1st body is executed for the 1st and last elements while the 2nd occurs for all others. And naturally if there are no extra bodies then just body is executed and the loop behaves as it traditionally has. I think doing things this way would get rid of a lot of repetition as it seems the user is often having to test if they are on the 1st or last elements.

cormullion

#1
I certainly like the $last idea.



As for the enhanced structure - it looks intriguing. Perhaps just implement the Common Lisp 'loop' form? Aaargh!

Dmi

#2
Vote for that!
WBR, Dmi

Lutz

#3
The length of a list is not known to 'dolist' (or any other function) when it starts iterating through the list. Same is true most of the time for the last element. It is stored sometimes in the envelope cell of the list, but cannot be found there reliably, but only after a previous access to the last cell.



Lutz