the 'broken' statement - (nf)

Henry Spencer henry at utzoo.UUCP
Sun Oct 16 14:27:24 AEST 1983


 "How would you specify the list?  Is it always an array? a linked list?
 a binary tree?  a completely specified list of simple [== non array]
 variables?  Have the compiler understand about all these data structures?
 Blech."

Agreed.  One of the major weaknesses of the FOR loop in languages like
Pascal is that it cannot be used to scan a non-trivial user-specified
data structure.  Both FOR and FIRST loops work much better in a language
like CLU or ALPHARD which explicitly recognizes that such a "scanning"
operation is an important part of a user-constructed data type, and needs
to be explicitly provided for.  The preferred approach is to be able to
define "generators" which scan a data structure and feed elements to a
FOR/FIRST loop for processing.  E.g., for a tree structure:

	FOR element in preorder(treevariable)
	...

	FIRST element in postorder(treevariable)
	...

Note that the generators are defined as part of the data type, and hence
you don't need to retype them (and introduce amusing new bugs) every time
you want them.	

If one wanted to introduce the FIRST loop to C (an idea I oppose, on
the grounds that it's a bit late), one could use the same approach as
C's FOR:

	first (x = 1; x < TOP; x++)
	...

Generators are better, but retrofitting a whole abstract-datatype system
to C is such a major operation that it's hard to call the result C.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry



More information about the Comp.lang.c mailing list