Overview

12 A short introduction to functional data structures

This chapter introduces immutable data structures as a natural extension of the immutability principles discussed earlier, especially in the context of concurrency and collections. The main idea is that when data cannot be mutated in place, updates produce new values instead of changing existing ones, which greatly reduces race conditions and makes reasoning about shared data much safer. Although this may sound expensive at first, the chapter shows that functional data structures can avoid full copying by reusing as much of the original structure as possible.

The first example is a classic singly linked list built from just two cases: an empty value and a non-empty node containing a head element and a tail list. Using this recursive representation, the chapter shows how to define a small but expressive API around pattern matching, including construction, length, mapping, aggregation, adding an element to the front, and retrieving the tail. These operations are efficient for front-oriented use cases because adding or removing the first element can be done in constant time, while other positions require traversal and are therefore linear in the portion inspected.

The chapter then applies the same style to binary trees, again using a recursive sum type with leaf and branch cases. With pattern matching, it becomes straightforward to define operations such as counting nodes, computing depth, mapping values across the tree, aggregating results, and inserting new elements. The chapter emphasizes structure sharing: when a tree is updated, unchanged subtrees are reused rather than rebuilt, which keeps immutable updates practical. It concludes by noting that these examples are intentionally simple and not production-ready, but they capture the core benefits of functional data structures: safety, composability, and often surprisingly good performance.

A linked list containing the values ["a", "b", "c"]
Adding an item to a list doesn’t affect the original list.
The Map function for a binary tree
The tree with the added value shares parts of the structure with the original tree.

Summary

  • In FP, collections should be immutable so that existing collections are never altered, but rather new collections are created with the desired changes.
  • Immutable collections can be safe and efficient because an updated version shares much of its structure with the original collection without affecting it.

FAQ

What is a functional data structure in this chapter’s context?A functional data structure is an immutable data structure: instead of changing an existing collection, operations return a new version with the desired changes. In the chapter, “functional” and “immutable” are used interchangeably.
Why are immutable collections useful when working with concurrency?Because they can’t be mutated after creation, immutable collections avoid race conditions caused by one thread changing data while another is reading or processing it. This makes shared data safer in concurrent programs.
How is a classic functional linked list represented?It is defined recursively as List<T> = Empty | Cons(T, List<T>). That means a list is either empty, or a head element plus another list as the tail.
What do “head” and “tail” mean in a linked list?The head is the first element in the list, and the tail is the rest of the list after the head. In a Cons, the head stores one value and the tail stores another List<T>.
Why does adding an element to an immutable linked list not require copying every item?Because the new list can reuse the existing list as its tail. Only one new Cons node is created for the added element, so the operation runs in constant time.
What does the Match method do for a functional linked list?Match provides pattern matching over the two possible cases of a list: empty or non-empty. It lets you define one function for the empty case and another for the Cons case, simplifying list operations.
How are common operations like Length, Map, and Aggregate implemented for the list?They are defined recursively using Match. The empty case returns a base value, and the non-empty case processes the head and then recursively handles the tail.
What is the main limitation of the linked-list implementation shown in the chapter?The examples are not stack-safe, so very long lists can cause a StackOverflowException. Also, the implementation is naive and intended for understanding concepts rather than production use.
What is structure sharing in immutable trees?Structure sharing means a modified tree reuses as much of the original tree’s structure as possible. For example, inserting into a tree creates only the nodes along the path to the insertion point while leaving the rest unchanged.
What is the binary tree definition introduced in the chapter?The tree is defined recursively as Tree<T> = Leaf(T) | Branch(Tree<T>, Tree<T>). A leaf contains a value, and a branch contains two subtrees.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free