Loopless functional algorithms

  • Authors:
  • Richard S. Bird

  • Affiliations:
  • Programming Research Group, Oxford University, Oxford, UK

  • Venue:
  • MPC'06 Proceedings of the 8th international conference on Mathematics of Program Construction
  • Year:
  • 2006

Quantified Score

Hi-index 0.00

Visualization

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.