Suggestions for DOLIST

For the Compleat Fan
Locked
Jeremy Dunn
Posts: 95
Joined: Wed Oct 13, 2004 8:02 pm
Location: Bellingham WA

Suggestions for DOLIST

Post by 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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

I certainly like the $last idea.

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

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Vote for that!
WBR, Dmi

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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

Locked