Persistent data structures for collections in Elm

Introduction

Elm crafts itself specifically as a functional programming language with the primary objective of constructing web applications. It focuses on simplicity, reliability, and maintainability. Elm has become famous for emphasizing simplicity, functional programming principles, and strong guarantees of type safety and immutability. It aims to provide web developers with a pleasant and productive experience by providing a reliable and maintainable way to build web applications.

Persistent data structures

The persistent data structures preserve the previous version of themselves when they modify themselves. In other words, once they create a version of the data structure, they remain unchanged, and any subsequent modifications create a new version of the data structure while leaving the original version intact. These are the main persistent data structures available in Elm:

  • List

  • Array

  • Set

  • Dict

Types of persistent data structures in Elm
Types of persistent data structures in Elm

The list module

A list is comprised of a gathering of homogeneous values, implying that all elements within the list share the same data type. Lists in Elm are immutable, so operations such as adding or removing elements return a new list while leaving the original list unchanged. Elm's built-in list module provides a persistent singly linked list. Functions like List.map, List.filter, and List.foldl enable us to manipulate lists.

Example:

{
    "type": "application",
    "source-directories": [
        "src"
    ],
    "elm-version": "0.19.1",
    "dependencies": {
        "direct": {
            "elm/browser": "1.0.2",
            "elm/core": "1.0.5",
            "elm/html": "1.0.0",
            "elm/json": "1.1.3"
        },
        "indirect": {
            "elm/time": "1.0.0",
            "elm/url": "1.0.0",
            "elm/virtual-dom": "1.0.3"
        }
    },
    "test-dependencies": {
        "direct": {},
        "indirect": {}
    }
}
A list example

The array module

Elm's array module provides a persistent array data structure. Arrays in Elm are indexed collections with constant access. They are helpful when you need efficient random access or want to change an element at a specific index. Like lists, Elm arrays are immutable, and operations such as updating or adding elements result in new arrays.

Example:

{
    "type": "application",
    "source-directories": [
        "src"
    ],
    "elm-version": "0.19.1",
    "dependencies": {
        "direct": {
            "elm/browser": "1.0.2",
            "elm/core": "1.0.5",
            "elm/html": "1.0.0",
            "elm/json": "1.1.3"
        },
        "indirect": {
            "elm/time": "1.0.0",
            "elm/url": "1.0.0",
            "elm/virtual-dom": "1.0.3"
        }
    },
    "test-dependencies": {
        "direct": {},
        "indirect": {}
    }
}
An array example

The set module

Elm's set module provides a persistent collection data structure. Sets in Elm are unordered collections that guarantee the uniqueness of elements. You can use functions such as Set.union, Set.intersect, and Set.diff to perform set operations such as join, intersection, and difference. Collections are immutable, so operations on collections generate new collections.

Example:

{
    "type": "application",
    "source-directories": [
        "src"
    ],
    "elm-version": "0.19.1",
    "dependencies": {
        "direct": {
            "elm/browser": "1.0.2",
            "elm/core": "1.0.5",
            "elm/html": "1.0.0",
            "elm/json": "1.1.3"
        },
        "indirect": {
            "elm/time": "1.0.0",
            "elm/url": "1.0.0",
            "elm/virtual-dom": "1.0.3"
        }
    },
    "test-dependencies": {
        "direct": {},
        "indirect": {}
    }
}
A set example

The dict module

Elm's dict module provides a persistent dictionary data structure. Dictionaries in Elm are key-value pairs, where each key is associated with a value. You can use functions such as Dict.insert, Dict.update, and Dict.remove to perform operations such as inserting, updating, and removing assets. Dictation is also immutable, and operations on dictionaries create new dictionaries.

Example:

{
    "type": "application",
    "source-directories": [
        "src"
    ],
    "elm-version": "0.19.1",
    "dependencies": {
        "direct": {
            "elm/browser": "1.0.2",
            "elm/core": "1.0.5",
            "elm/html": "1.0.0",
            "elm/json": "1.1.3"
        },
        "indirect": {
            "elm/time": "1.0.0",
            "elm/url": "1.0.0",
            "elm/virtual-dom": "1.0.3"
        }
    },
    "test-dependencies": {
        "direct": {},
        "indirect": {}
    }
}
A dict example

Persistent data structures in Elm provide referential transparency and make it easier to reason about program behavior. They provide safe and predictable updates without changing existing data, help avoid unintended side effects, and facilitate code reuse.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved