[net.lang] loop indexing

mac@uvacs.UUCP (01/12/84)

Re: superstitions about FOR ... IN [ REVERSE ] .... LOOP ... END LOOP;

It would be nice to have a repitition construct for those loops where it
doesn't matter whether you count up or down.  Something like Algol68's
	 TO iteration count DO loop stuff OD

On the other hand, sometimes you need a loop variable (e.g. array index)
but don't care what order it runs.  I don't recall seeing anything like this.

This is something I'd like to be able to let the compiler decide.  As it
is, I'm forced to tell the compiler what order to use.

rpw3@fortune.UUCP (01/16/84)

#R:uvacs:-112200:fortune:15100008:000:851
fortune!rpw3    Jan 15 20:24:00 1984

In some languages, you can say things like

	for all x in {set/range/list} do ...;

so to add all the values of a singly-dimensioned array

	s := 0;
	for all i in { lowbound(a):highbound(a) } do s := s + a[i];

Notice that the compiler can use random order, if it likes, as long
as it covers all of the entries.  These things are in the class of
language features called "generators". Generators were introduced
exactly to address the point of the previous note: We sometimes need
ways to indicate to the compiler a set of computations to be done,
without implying an ordering to that set.

(I'm afraid I've forgotten what languages use generators, other than
SCHEME's "streams".)

Rob Warnock

UUCP:	{sri-unix,amd70,hpda,harpo,ihnp4,allegra}!fortune!rpw3
DDD:	(415)595-8444
USPS:	Fortune Systems Corp, 101 Twin Dolphins Drive, Redwood City, CA 94065

norskog@fortune.UUCP (Lance Norskog) (01/16/84)

A more generalized form of this is:

	for x,y in sequence(x,y) 
		Fragment
	rof

	.
	.
	.


	GENERATOR sequence(a:int,b:boolean)
		loop
			.
			.
			yield a,b
			.
			.
		pool
	ROTARENEG

Every 'yield' is implemented as a procedure call: Fragment(a,b).
When 'sequence' returns, the for loop is over.

The only place I have seen this is in academic "toy" languages;
the only implemented version I have used was the language
"Small" in a compiler class; we had to add features
to a skeletal language and its parser/compiler/interpreter
system.  I thought generators (we called them "iterators")
were cute so I implemented them as my project.  
They are a halfway step to object-oriented programming in a procedural language.

wje@sii.UUCP (Bill Ezell) (01/17/84)

b
Avl (an applications implementation language for Unix) provides a
generator facility:

	foreach(a,b)
	{
	..
	}

which means 'for each a in b'. Since the basic datatype is a tree of trees,
foreach is very useful. Because the compiler optimizes references to 'a' to
be pointers into 'b', execution is much faster than explicit iteration.

I have found this to be very natural way to deal with sets of data; it is
surprisingly rare to actually need to know the order in which things
are manipulated.

Note that Avl has no explicit pointers; the compiler hides the fact that
'a' is a pointer. This allows the efficiency gained by pointer manipulation
without the complexity (and confusion) commonly associated with explicit
pointer datatypes. 'a' is treated specially only within the scope of the
foreach.

		-Bill Ezell
		 Software Innovations, Inc.
		 (decvax!sii!wje)
		 (ittvax!sii!wje)


PS: Incidentially, Avl isn't a 'toy' language; it is currently being used
by OEM's for real applications programming.

andree@uokvax.UUCP (01/21/84)

#R:uvacs:-112200:uokvax:9000022:000:616
uokvax!andree    Jan 19 19:16:00 1984

The iterators fortune!norskog describes are almost identical to the
generators of CLU (someone more familiar with CLU please correct me if
I'm wrong).

On the other hand, the full TO loop syntax from ALGOL68 (as I remember it)
is:

	[FOR var] [FROM count] TO count [STEP size] DO
		fragment
		OD

The only required part was the TO count, which caused fragment to be
executed count times. All the others could be added at will, giving
starting point, step size, and a variable if you needed it in the loop.

I like it much better than any other counter/loop I've seen. If only it
didn't have that DO OD pair.

	<mike