## The Under-Appreciated Unfold (1998)

### Cached

### Download Links

- [web.comlab.ox.ac.uk]
- [ftp.comlab.ox.ac.uk]
- DBLP

### Other Repositories/Bibliography

Venue: | In Proceedings of the Third ACM SIGPLAN International Conference on Functional Programming |

Citations: | 48 - 10 self |

### BibTeX

@INPROCEEDINGS{Gibbons98theunder-appreciated,

author = {Jeremy Gibbons and Geraint Jones},

title = {The Under-Appreciated Unfold},

booktitle = {In Proceedings of the Third ACM SIGPLAN International Conference on Functional Programming},

year = {1998},

pages = {273--279},

publisher = {ACM Press}

}

### Years of Citing Articles

### OpenURL

### 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.

### Citations

1309 | Monads for Functional Programming - Wadler - 1995 |

466 |
The Science of Programming
- Gries
- 1981
(Show Context)
Citation Context ...e tree, so the program is quadratic. To make it take linear time, we could use a clever data structure that allows queue operations in amortized constant time [12, 20], but here the simpler technique =-=[5, 9, 11]-=- of using two lists, one reversed, suffices. That is, the idea is to introduce a function bftf' such that bftf' (ts,vs) = bftf (ts ++ reverse vs) where reverse reverses a list; then bftf ts = bftf' (t... |

400 | Deforestation: Transforming programs to eliminate trees
- Wadler
- 1990
(Show Context)
Citation Context ...impler) than the characterization as a fold, but it is more efficient. Moreover, we will see subsequently that it is also amenable to manipulation; to conclude this section, we will use deforestation =-=[25]-=- to eliminate the intermediate list of lists constructed during the breadthfirst traversal. 5.1 Deforestation As hinted at above, one of the benefits that accrues from expressing levelf as an unfold i... |

298 | Functional programming with bananas, lenses, envelopes and barbed wire
- Meijer, Fokkinga, et al.
- 1991
(Show Context)
Citation Context ...g recursion directly are well-known and well understood [14]. The dual notion to folds, unfolds, have been explored by Hagino [10] and Malcolm [16], and popularized at this conference by Meijer et al =-=[18]-=-. Unfolds are certainly not new, but they are not nearly as well appreciated as folds. (For example, they merit just half a page in [4], and have disappeared altogether in [3]. Co-inductive types warr... |

285 | Why Functional Programming Matters
- Hughes
- 1990
(Show Context)
Citation Context ...re appreciated by functional programmers. The benefits of encapsulating common patterns of computation as higher-order operators instead of using recursion directly are well-known and well understood =-=[14]-=-. The dual notion to folds, unfolds, have been explored by Hagino [10] and Malcolm [16], and popularized at this conference by Meijer et al [18]. Unfolds are certainly not new, but they are not nearly... |

215 |
Introduction to Functional Programming using Haskell, 2nd edition
- Bird
- 1998
(Show Context)
Citation Context ...ference by Meijer et al [18]. Unfolds are certainly not new, but they are not nearly as well appreciated as folds. (For example, they merit just half a page in [4], and have disappeared altogether in =-=[3]-=-. Co-inductive types warrant a few pages in [23], but apart from that there are no other mentions in the fourteen functional programming textbooks on our shelves.) We believe unfolds deserve a much hi... |

105 |
Data structures and program transformation
- Malcolm
- 1990
(Show Context)
Citation Context ... of computation as higher-order operators instead of using recursion directly are well-known and well understood [14]. The dual notion to folds, unfolds, have been explored by Hagino [10] and Malcolm =-=[16]-=-, and popularized at this conference by Meijer et al [18]. Unfolds are certainly not new, but they are not nearly as well appreciated as folds. (For example, they merit just half a page in [4], and ha... |

97 | Type Theory and Functional Programming
- Thompson
- 1991
(Show Context)
Citation Context ...inly not new, but they are not nearly as well appreciated as folds. (For example, they merit just half a page in [4], and have disappeared altogether in [3]. Co-inductive types warrant a few pages in =-=[23]-=-, but apart from that there are no other mentions in the fourteen functional programming textbooks on our shelves.) We believe unfolds deserve a much higher profile. To illustrate this claim, we prese... |

87 |
The Promotion and Accumulation Strategies in Transformational Programming
- Bird
- 1984
(Show Context)
Citation Context ...every parent appears in the traversal before any of its children, and siblings appear in left-to-right order. For example, the preorder traversal of the tree 2 k 1 k 2 k 5 k 6 k 3 k 4 k 7 is the list =-=[1,2,5,6,3,4,7]-=-. Preorder traversal, postorder traversal (in which a parent appears after all its children) and inorder traversal (which only makes sense on binary trees, and in which a parent appearssbetween its tw... |

55 | Continuation-based program transformation strategies
- Wand
- 1980
(Show Context)
Citation Context ...le to regain a characterization as a fold, but taking linear time, by abstracting from the accumulating parameter and constructing instead a function between lists 4 of lists, in a continuation-based =-=[26] or higher-order fol-=-d [6] style: levelt" :: Tree a -? [[a]] -? [[a]] levelt" = foldt f g where f a hss = (a:) : hss g = foldr (lzw (.)) [] levelf" :: Forest a -? [[a]] -? [[a]] levelf" = foldf f g but... |

54 | Revisiting catamorphism over datatypes with embedded functions - Fegaras, Sheard - 1996 |

46 | A typed lambda calculus with categorical type constructors
- Hagino
- 1987
(Show Context)
Citation Context ...g common patterns of computation as higher-order operators instead of using recursion directly are well-known and well understood [14]. The dual notion to folds, unfolds, have been explored by Hagino =-=[10]-=- and Malcolm [16], and popularized at this conference by Meijer et al [18]. Unfolds are certainly not new, but they are not nearly as well appreciated as folds. (For example, they merit just half a pa... |

25 | Simple and efficient purely functional queues and deques
- Okasaki
- 1995
(Show Context)
Citation Context ...s, which grows linearly in the size of the tree, so the program is quadratic. To make it take linear time, we could use a clever data structure that allows queue operations in amortized constant time =-=[12, 20]-=-, but here the simpler technique [5, 9, 11] of using two lists, one reversed, suffices. That is, the idea is to introduce a function bftf' such that bftf' (ts,vs) = bftf (ts ++ reverse vs) where rever... |

24 |
Algebras for Tree Algorithms. D
- Gibbons
- 1991
(Show Context)
Citation Context ...every parent appears in the traversal before any of its children, and siblings appear in left-to-right order. For example, the preorder traversal of the tree 2 k 1 k 2 k 5 k 6 k 3 k 4 k 7 is the list =-=[1,2,5,6,3,4,7]-=-. Preorder traversal, postorder traversal (in which a parent appears after all its children) and inorder traversal (which only makes sense on binary trees, and in which a parent appearssbetween its tw... |

16 |
Efficient functional implementation of FIFO queues
- Burton
- 1982
(Show Context)
Citation Context ...every parent appears in the traversal before any of its children, and siblings appear in left-to-right order. For example, the preorder traversal of the tree 2 k 1 k 2 k 5 k 6 k 3 k 4 k 7 is the list =-=[1,2,5,6,3,4,7]-=-. Preorder traversal, postorder traversal (in which a parent appears after all its children) and inorder traversal (which only makes sense on binary trees, and in which a parent appearssbetween its tw... |

12 |
A symmetric set of efficient list operations
- Hoogerwoord
- 1992
(Show Context)
Citation Context ...s, which grows linearly in the size of the tree, so the program is quadratic. To make it take linear time, we could use a clever data structure that allows queue operations in amortized constant time =-=[12, 20]-=-, but here the simpler technique [5, 9, 11] of using two lists, one reversed, suffices. That is, the idea is to introduce a function bftf' such that bftf' (ts,vs) = bftf (ts ++ reverse vs) where rever... |

9 |
Real-time queue operations in pure lisp
- Hood, Melville
- 1981
(Show Context)
Citation Context ...e tree, so the program is quadratic. To make it take linear time, we could use a clever data structure that allows queue operations in amortized constant time [12, 20], but here the simpler technique =-=[5, 9, 11]-=- of using two lists, one reversed, suffices. That is, the idea is to introduce a function bftf' such that bftf' (ts,vs) = bftf (ts ++ reverse vs) where reverse reverses a list; then bftf ts = bftf' (t... |

9 |
First steps towards the theory of rose trees
- Meertens
- 1987
(Show Context)
Citation Context ... by folds, so folds and unfolds cannot be composed; in cpo, the two kinds of data structure are the same. 2.3 Trees Our trees are represented by the datatype data Tree a = Nd a [Tree a] of rose trees =-=[17]-=-. That is, a tree of type Tree a consists of a root label of type a and a list of children, each again of type Tree a. We define the two deconstructors root and kids: root :: Tree a -? a root (Nd a ts... |

7 | Linear-time breadth-first tree algorithms: An exercise in the arithmetic of folds and zips
- Jones, Gibbons
- 1993
(Show Context)
Citation Context ...wledgements This paper arose from a discussion late in 1992 on the Usenet newsgroup comp.lang.functional, on how to perform breadth-first traversal in a functional language. We wrote an earlier paper =-=[15]-=- giving derivations of our solutions, and also giving a rather complicated derivation of an algorithm for breadth-first labelling, a kind of inverse of breadth-first traversal originally posed to us b... |

6 | Deriving tidy drawings of trees
- Gibbons
- 1996
(Show Context)
Citation Context ...ification' of the problem (even if we then develop a more operational implementation). We find this more declarative characterization of breadth-first traversal in the notion of level-order traversal =-=[7, 8]-=- of a tree. This gives the elements on each level of the tree, as a list of lists (and so, strictly speaking, this is not a traversal according to our definition). For example, the level-order travers... |

4 |
Masato Takeichi. Deriving structural hylomorphisms from recursive definitions
- Hu, Iwasaki
- 1996
(Show Context)
Citation Context ...two phases into one, to reduce the amount of heap space turned over. This transformation is known as deforestation [25], and is now a standard technique; indeed, it can even be performed mechanically =-=[13, 21]-=-. To be specific, we will use deforestation on functions of the form h = foldr op e . unfold p f g Consider first the case that p holds of the argument: h x = f h g foldr op e (unfold p f g x) = f ass... |

2 | Addendum to "The promotion and accumulation strategies in transformational programming - Bird - 1985 |

2 |
The Haskell 1.4 report. http://www.haskell.org/report
- Peterson, Hammond, et al.
- 1997
(Show Context)
Citation Context ...ion as a fold, and show that it is linear. Finally, in Section 6 we calculate as an unfold the standard queue-based algorithm for breadth-first traversal. 2 Notation We will be using Haskell notation =-=[22]-=-, but the translation into nearly any modern functional language is straightforward. 2.1 Folds over lists We will be using two kinds of fold on lists: the normal `fold right', foldr :: (a-?b-?b) -? b ... |