CS 15-212: Fundamental Structures of Computer Science II
We expand on our "purely functional" techniques by introducing methods for designing ephemeral data structures, which use mutable storage to maintain internal state. Three examples are used to demonstrate some of the ideas. First, arrays are used to implement generic hash tables as a functor parameterized by an abstract hashable equality type. This closely parallels the natural implementation of hash tables in many imperative languages. Next, we revisit the queue data structure by defining an ephemeral queue, where the signature clearly indicates that internal state is maintained. Our implementation uses a pair of reference cells containing mutable lists, and highlights some of the subtleties involved when reasoning about references. Finally, we show how persistent data structures may sometimes be manipulated using mutable storage by presenting a functional version of splay trees, a type of self-adjusting binary search tree.