Tu

Tuples in Elm

6 exercises

About Tuples

Tuples in Elm can hold two or three values, and each value can have any type. In Elm, Tuples are mostly used for simple transient types. For this reason Tuples have a maximum of 3 values, and as a further encouragement convenience functions are only supplied for 2 values. For more complex data, it is best to switch to records. A common use is if you need to return more than one value from a function. Tuple fields don't have names, they are accessed by means of destructuring or by position.

You can read a concise introduction to tuples. There is also a more in depth introduction, and the API doc of the Tuple module contains code examples and detailed documentation.

Declaration

Types for Tuples are created with parentheses and their element types are separated by commas.

type alias IntStringTuple = (Int, String)

Creation

Tuples are created with parentheses and their elements are separated by commas.

aTuple : IntStringTuple
aTuple = (123, "elements can be of differing types")

You can also use Tuple.pair to create a tuple with two values.

anotherTuple : (String, String)
anotherTuple = Tuple.pair "this time it's" "two strings"
    --> ("this time it's", "two strings")

Access

There are also functions to access tuple values by position, Tuple.first and Tuple.second.

Tuple.first aTuple
    --> 123

Tuple.second aTuple
    --> "elements can be of differing types"

Tuples can be destructured in bindings using tuple pattern matching. Destructuring can be done in let expressions, function declarations and case statements.

repeatString : (Int, String) -> Bool
repeatString aTuple =
let
    (aNumber, aString) = aTuple
in
    String.repeat aNumber aString

repeatStringAgain : (Int, String) -> Bool
repeatStringAgain (aNumber, aString) = String.repeat aNumber aString

Pattern matching in a case statement also allows you to match on content of the values:

has123 : (Int, String, Bool) -> Bool
has123 aTuple =
    case aTuple of
        (123, _, _) ->
            True
        (_, "123", _) ->
            True
        (_, _, True) ->
            True
        _ ->
            False
Edit via GitHub The link opens in a new window or tab

Learn Tuples

Practicing is locked

Unlock 1 more exercise to practice Tuples