ABSTRACT
We present a programming pattern where a recursive function traverses a data structure---typically a list---at return time. The idea is that the recursive calls get us there (typically to a base case) and the returns get us back again while traversing the data structure. We name this programming pattern of traversing a data structure at return time "There And Back Again" (TABA).The TABA pattern directly applies to computing a symbolic convolution. It also synergizes well with other programming patterns, e.g., dynamic programming and traversing a list at double speed. We illustrate TABA and dynamic programming with Catalan numbers. We illustrate TABA and traversing a list at double speed with palindromes and we obtain a novel solution to this traditional exercise.A TABA-based function written in direct style makes full use of an Algol-like control stack and needs no heap allocation. Conversely, in a TABA-based function written in continuation-passing style, the continuation acts as a list iterator. In general, the TABA pattern saves one from constructing intermediate lists in reverse order.
- Richard S. Bird. Using circular programs to eliminate multiple traversals of data. Acta Informatica, 21:239--250, 1984.Google ScholarDigital Library
- William Clinger, Anne H. Hartheimer, and Eric M. Ost. Implementation strategies for first-class continuations. Higher-Order and Symbolic Computation, 12(1):7--45, 1999. Google ScholarDigital Library
- Olivier Danvy and Andrzej Filinski. Representing control, a study of the CPS transformation. Mathematical Structures in Computer Science, 2(4):361--391, 1992.Google ScholarCross Ref
- Olivier Danvy and Julia L. Lawall. Back to direct style II: First-class continuations. In William Clinger, editor, Proceedings of the 1992 ACM Conference on Lisp and Functional Programming, LISP Pointers, Vol. V, No. 1, pages 299--310, San Francisco, California, June 1992. ACM Press. Google ScholarDigital Library
- Ronald L. Graham, Donald E. Knuth, and Oren Patashnik. Concrete Mathematics. Addison-Wesley, 1989.Google Scholar
- Luo Jian-Jin. Catalan numbers in the history of mathematics in China. In H.P. Yap et al., editor, Combinatorics and Graph Theory: Proc. Spring School and International Conference on Combinatorics, pages 68--70, Hefei, China, 1993. World Scientific.Google Scholar
- John Launchbury and Tim Sheard. Warm fusion: Deriving build-cata's from recursive definitions. In Simon Peyton Jones, editor, Proceedings of the Seventh ACM Conference on Functional Programming and Computer Architecture, pages 314--323, La Jolla, California, June 1995. ACM Press. Google ScholarDigital Library
- Y. Annie Liu and Scott D. Stoller. From recursion to iteration: what are the optimizations? In Julia L. Lawall, editor, Proceedings of the 2000 ACM SIGPLAN Workshop on Partial Evaluation and Semantics-Based Program Manipulation, SIGPLAN Notices, Vol. 34, No~11, pages 73--82, Boston, Massachusetts, November 2000. ACM Press. Google ScholarDigital Library
- Alberto Pettorossi and Maurizio Proietti. Importing and exporting information in program development. In Dines Bjørner, Andrei P. Ershov, and Neil D. Jones, editors, Partial Evaluation and Mixed Computation, pages 405--425. North-Holland, 1988.Google Scholar
- Alberto Pettorossi and Maurizio Proietti. A comparative revisitation of some program transformation techniques. In Olivier Danvy, Robert Glück, and Peter Thiemann, editors, Partial Evaluation, number 1110 in Lecture Notes in Computer Science, pages 355--385, Dagstuhl, Germany, February 1996. Springer-Verlag. Google ScholarDigital Library
- Jagadguru Swami Sri Bharati Krsna Tirthaji Maharaja. Vedic Mathematics. Motilal Banarsidass Publishers Private Limited, 1992.Google Scholar
- Guy L. Steele Jr. Debunking the `expensive procedure call' myth. In Proceedings of the ACM National Conference, pages 133--162, Seattle, Washington, October 1977. Extended version available as MIT AI Memo 443. Google ScholarDigital Library
- Guy L. Steele Jr. Common Lisp: The Language. Digital Press, 1984. Google ScholarDigital Library
- Robert A. Sulanke. Moments of generalized Motzkin paths. Journal of Integer Sequences, 3(00.1.1), 2000. http://www.math.uwaterloo.ca/JIS/.Google Scholar
- Hongwei Xi and Frank Pfenning. Dependent types in practical programming. In Alex Aiken, editor, Proceedings of the Twenty-Sixth Annual ACM Symposium on Principles of Programming Languages, pages 214--227, San Antonio, Texas, January 1999. ACM Press. Google ScholarDigital Library
Index Terms
- There and back again
Recommendations
There and back again
We present a programming pattern where a recursive function traverses a data structure---typically a list---at return time. The idea is that the recursive calls get us there (typically to a base case) and the returns get us back again while traversing ...
Bijections for Baxter families and related objects
The Baxter number B"n can be written as B"n=@?"k"="0^n@Q"k","n"-"k"-"1 with@Q"k","@?=2(k+1)^2(k+2)(k+@?k)(k+@?+1k)(k+@?+2k). These numbers have first appeared in the enumeration of so-called Baxter permutations; B"n is the number of Baxter permutations ...
More restrictive Gray codes for some classes of pattern avoiding permutations
In a recent article [W.M.B. Dukes, M.F. Flanagan, T. Mansour, V. Vajnovszki, Combinatorial Gray codes for classes of pattern avoiding permutations, Theoret. Comput. Sci. 396 (2008) 35-49], Dukes, Flanagan, Mansour and Vajnovszki present Gray codes for ...
Comments