Programming with Generators

Ralph E. Griswold · The Computer Journal · 1988

The facilities that a programming language provides effectively both specify and limit how programs in the language can be written. Programming languages with unusual facilities often lead to program formulations that would be difficult or unnatural in other programming languages. This paper describes the characteristics and use of such a facility: expressions that are capable of producing a sequence of results, not just a single result. Such expressions are called generators. Generators are described here in the context of the Icon programming language, in which they are a general feature, instead of being confined to specific data structures or particular contexts as they are in other programming languages. In Icon, generators arise naturally from computations in which there is more than one possible solution. Coupled with goal-directed evaluation, this leads to concise and natural formulations of many types of computation. In turn, control structures are needed for generators that have no counterpart in more conventional programming languages. Numerous examples of generators are given, together with some general paradigms for their use. From a more formal point of view, the capability of generators to produce results is characterized in terms of result sequences. Finally, the relationship between generators and logic programming is mentioned.

Read the paper · More papers on PaperTik