Affordable Access

Publisher Website

From Recursion to Iterators-Chapter 5

Elsevier Inc.
DOI: 10.1016/b978-155860701-9/50005-2


Publisher Summary This chapter discusses the techniques for transforming general recursive functions into iterators. Iterators are useful when a source of data is prepared to deliver more data than is needed, or when it takes a long time to come up with each data item. Both conditions occur frequently in conjunction with recursive functions. Recursive functions are often used for searching large, hierarchical spaces for solutions to some specification. If solutions are common, the space will contain more of them than is needed for the usage; if solutions are rare, they will take a long time to be found. In either case, it is unlikely that the program have to be populated in an array with all the possible solutions before it can continue, and it is natural to use an iterator. Recursion elimination may be useful in reducing the memory footprint of a function. With an iterator solution, the queue can be ordered and even reordered dynamically when new information arrives. But recursive functions are often easy to write, whereas iterators seemed to require ingenuity. The chapter also explains the way to make a generic tree-search iterator. The two essential requirements for this construction function are the root node and a callback function. The techniques required to convert a recursive function to an iterator are the odometer method, the agenda method, tail-call elimination, creating tail calls, and explicit stacks,

There are no comments yet on this publication. Be the first to share your thoughts.