Path: utzoo!utgpu!watmath!att!tut.cis.ohio-state.edu!ucbvax!agate!shelby!csli!poser From: poser@csli.Stanford.EDU (Bill Poser) Newsgroups: comp.lang.c Subject: Re: Coroutines in C Summary: Coroutine facilities are related to procedure suspension Message-ID: <10097@csli.Stanford.EDU> Date: 15 Aug 89 22:45:34 GMT References: <5663@ficc.uu.net> <14281@haddock.ima.isc.com> <563@augean.OZ> <5695@ficc.uu.net> Sender: poser@csli.Stanford.EDU (Bill Poser) Reply-To: poser@csli.stanford.edu (Bill Poser) Organization: Center for the Study of Language and Information, Stanford U. Lines: 19 Another application of the same sorts of facilities used to implement coroutines is for generator functions, i.e. functions that generate a new member of some set each time they are called. Such functions are useful for pattern matching. I have also found them useful when I needed to obtain strings contained in heterogeneous data structures (e.g. arrays and hash tables containing different kinds of objects) and sort and format information coming from all of them. To get each string you call the generator function for one of the types of data. The generator functions know the details of how the data is stored and how to traverse the data structure. But they all return objects of the same type (strings) and you keep control over storage allocation, sorting, formatting etc. in one place. Generator functions can be implemented by hand by using static variables to save the necessary state information, but this gets hairy very quickly. If one can suspend the function rather than return from it (which means, essentially, saving the stack frame) generators easy to write. This is a nice facility of ICON.