[comp.lang.clu] Recursive iterator in CLU?

brock@relay.EU.net (Inst.f.Prakt.Info 1802) (10/01/89)

CLU's explicit concept of iterators seems at the first glance preferable
to the rather ad hoc way iterators are defined in oo -languages. (As
a method of an object, or as a friend-class.) Instead of handling
the whole state of the iterator explicitely, one can imagine the yield
statement as yust a "print" statement which will pe "piped" to the caller.
But this works for simple examples only. If you want to traverse e.g. a binary
tree you have to call within the iterator the same iterator for both left and
right subtree. In my opinion it would be more intuitive to use the "print"
methapher for all kinds of calls.

Has there anybody extended iterators in such a way?

Ulrich

scc@cl.cam.ac.uk (Stephen Crawley) (10/04/89)

Ulrich Neumerkel writes:
> CLU's explicit concept of iterators seems at the first glance preferable
> to the rather ad hoc way iterators are defined in oo -languages. (As
> a method of an object, or as a friend-class.) Instead of handling
> the whole state of the iterator explicitely, one can imagine the yield
> statement as yust a "print" statement which will pe "piped" to the caller.
> But this works for simple examples only. If you want to traverse e.g. a 
> binary tree you have to call within the iterator the same iterator for 
> both left and right subtree. In my opinion it would be more intuitive 
> to use the "print" methapher for all kinds of calls.

Recursive iterators are no problem in CLU.  For example: 

tree_node = cluster [T: type] is tree_walk ...

rep = struct [
  left:		node_or_leaf,
  right:	node_or_leaf]
  
node_or_leaf = oneof [
  a_node:	tree_node,
  a_leaf:	T]

tree_walk = iter (node: cvt) yields (T)
  tagcase node.left
    tag a_node (n: tree_node):
      for leaf: T in tree_walk(n) do yield (leaf) end
    tag a_leaf (leaf: T):
      yield (leaf)
    end
  tagcase node.right
    tag a_node (n: tree_node):
      for leaf: T in tree_walk(n) do yield (leaf) end
    tag a_leaf (leaf: T):
      yield (leaf)
    end
  end
  
..

end tree_node

Frankly, I don't see what is non-intuitive about this use of recursion.  
The CLU compiler may do a poor job of optimising recursive iterators ... 
but that is a different issue entirely.

-- Steve