Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!ucsd!ucbvax!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!mcsun!sunic!tut!ra!tucos!moj From: scc@cl.cam.ac.uk (Stephen Crawley) Newsgroups: comp.lang.clu Subject: Re: Recursive iterator in CLU? Keywords: iterators Message-ID: <934@scaup.cl.cam.ac.uk> Date: 3 Oct 89 22:53:12 GMT References: <730@tuvie> Sender: moj@tucos.UUCP Distribution: inet Organization: U of Cambridge Comp Lab, UK Lines: 47 Approved: moj@cs.utu.fi X-Posting-Number: 49 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