Li

Lists in Elm

42 exercises

About Lists

A List in Elm is an immutable collection of zero or more values of the same type. As lists are immutable, once a list has been constructed, its value can never change. Elm lists have a head (the first element) and a tail (everything after the first element). The tail of a list is itself a list.

Type annotations for lists can be defined as follows

List String --> a list of String
List Int --> a list of Int

Lists can be defined as follows:

empty : List Char
empty = []

singleValue = [ 5 ] --> List Int
singleValueAlternative = List.singleton 5 --> List Int

threeValues = [ "a", "b", "c" ] --> List String

The most common way to add an element to a list is through the :: (cons) operator:

twoToFour = [ 2, 3, 4 ]
oneToFour = 1 :: twoToFour --> [1; 2; 3; 4]

List can be appended using the ++ operator or List.append:

[ 6, 7 ] ++ [ 8, 9 ]           --> [6, 7, 8, 9]
List.append [ 6, 7 ] [ 8, 9 ]  --> [6, 7, 8, 9]

Lists are manipulated by functions and operators defined in the List module.

List.length [ 7, 8 ] --> 2

Any function/operator that appear to modify a list (such as adding an element), will actually return a new list. Performance is usually not an issue though, as the implementation of lists prevents unnecessary allocations/copies.

Lists can also be processed using pattern matching.

describe : List String -> String
describe list =
    case list of
        [] ->
            "Empty list"

        [ "hello" ] ->
            "Singleton list with: hello"

        x :: xs ->
            "Non-empty list with head: " ++ x

describe []                   --> "Empty list"
describe [ "hello" ]          --> "Singleton list with: hello"
describe [ "hello", "world" ] --> "Non-empty list with head: hello"
Edit via GitHub The link opens in a new window or tab

Learn Lists

Practicing is locked

Unlock 9 more exercises to practice Lists