# Loopless Functional Algorithms

• Richard S. Bird
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 4014)

## Abstract

A loopless algorithm is a procedure for generating a list of values under two restrictions: the first element should be produced in linear time and each subsequent element in constant time. Loopless algorithms arise in the enumeration of combinatorial patterns such as permutations or subsequences. The elements of the generated list describe transitions that determine how the next combinatorial pattern is to be determined from its predecessor. Loopless algorithms were introduced in a procedural setting, and many clever tricks, such as focus pointers, doubly-linked lists and coroutines, have been used to construct them. This paper explores what a purely functional approach can bring to the subject, and calculates loopless functional versions of the Gray code algorithm, the Koda-Ruskey algorithm for listing the prefixes of a forest, and the Johnson-Trotter algorithm for generating permutations. The functional algorithms are completely different from their procedural counterparts, and rely on nothing more fancy than lists, trees and queues.

## Keywords

Binary Tree Composition Operator Functional Programming Gray Code Combinatorial Pattern
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.

## References

1. 1.
Bird, R.S.: Introduction to Functional Programming Using Haskell. Prentice Hall, Englewood Cliffs (1998)Google Scholar
2. 2.
Ehrlich, G.: Loopless algorithms for generating permutations, combinations, and other combinatorial configurations. J. of ACM 20, 500–513 (1973)
3. 3.
Filliatre, J.-C., Pottier, F.: Producing all ideals of a forest, functionally. J. of Funct. Program. 13(5), 945–956 (2003)
4. 4.
5. 5.
Knuth, D.E.: The Art of Computer Programming, vol. 4 (2004), www-cs-faculty.stanford.edu/~knuth/
6. 6.
Koda, Y., Ruskey, R.: A Gray code for the ideals of a forest poset. J. of Algorithms 15, 324–340 (1993)
7. 7.
Okasaki, C.: Simple and efficient purely functional queues and deques. J. of Funct. Program. 5(4), 583–592 (1995)