The under-appreciated unfold

  • Authors:
  • Jeremy Gibbons;Geraint Jones

  • Affiliations:
  • School of Computing and Math. Sciences, Oxford Brookes University, Gipsy Lane, Headington, Oxford 0X3 OBP, UK;Oxford University Computing Lab, Wolfson Building, Parks Road, Oxford OX1 3QD, UK

  • Venue:
  • ICFP '98 Proceedings of the third ACM SIGPLAN international conference on Functional programming
  • Year:
  • 1998

Quantified Score

Hi-index 0.00

Visualization

Abstract

Folds are appreciated by functional programmers. Their dual, unfolds, are not new, but they are not nearly as well appreciated. We believe they deserve better. To illustrate, we present (indeed, we calculate) a number of algorithms for computing the breadth-first traversal of a tree. We specify breadth-first traversal in terms of level-order traversal, which we characterize first as a fold. The presentation as a fold is simple, but it is inefficient, and removing the inefficiency makes it no longer a fold. We calculate a characterization as an unfold from the characterization as a fold; this unfold is equally clear, but more efficient. We also calculate a characterization of breadth-first traversal directly as an unfold; this turns out to be the 'standard' queue-based algorithm.