Esc
Start typing to search...

List Module

Functions for working with lists — the primary collection type in Keel.

Lists are ordered, homogeneous collections written as [1, 2, 3]. All elements must share the same type (List Int, List String, etc.). Lists are immutable — operations return new lists rather than modifying the original.

Common patterns

import List

-- Transform elements
List.map (|x| x * 2) [1, 2, 3]           -- [2, 4, 6]

-- Filter and chain with pipes
[1, 2, 3, 4, 5]
    |> List.filter (|x| x > 2)
    |> List.map (|x| x * 10)              -- [30, 40, 50]

-- Reduce to a single value
List.foldl (|acc x| acc + x) 0 [1, 2, 3]  -- 6
List.sum [1, 2, 3, 4]                      -- 10

-- Safe access (returns Maybe)
List.head [10, 20, 30]                     -- Just 10
List.head []                               -- Nothing

-- Build lists
List.range 1 5                             -- [1, 2, 3, 4, 5]
List.repeat 3 "hi"                         -- ["hi", "hi", "hi"]

Type variables

Many List functions use type variables:

  • a, b — any type (e.g. map : (a -> b) -> List a -> List b)
  • numberInt or Float (e.g. sum : List number -> number)
  • comparable — types that support ordering (e.g. sort : List comparable -> List comparable)

Functions

Transforming

List.map

(a -> b) -> [a] -> [b]

Apply a function to each element of a list, returning a new list with the results.

Example:
import List

List.map (|x| x * 2)
[1, 2, 3]

-- [2, 4, 6]
Try it

See also: List.filter, List.foldl

List.filter

(a -> Bool) -> [a] -> [a]

Keep only elements that satisfy the predicate.

Example:
import List

List.filter (|x| x > 2)
[ 1
, 2
, 3
, 4
]

-- [3, 4]
Try it

See also: List.map, List.find, List.partition

List.zip

[a] -> [b] -> [(a, b)]

Pair up elements from two lists into a list of tuples.

Example:
import List

List.zip [1, 2]
["a", "b"]

-- [(1, "a"), (2, "b")]
Try it

Notes: Stops at the shorter list.

See also: List.zipWith

List.zipWith

(a -> b -> c) -> [a] -> [b] -> [c]

Combine elements from two lists using a function.

Example:
import List

List.zipWith (|a b| a + b)
[1, 2]
[10, 20]

-- [11, 22]
Try it

Notes: Stops at the shorter list.

See also: List.zip, List.map

List.intersperse

a -> [a] -> [a]

Insert a value between each element of a list.

Example:
import List

List.intersperse 0
[1, 2, 3]

-- [1, 0, 2, 0, 3]
Try it

See also: String.join

Reducing

List.foldl

(b -> a -> b) -> b -> [a] -> b

Left fold: apply function to accumulator and each element from left to right.

Example:
import List

List.foldl (|acc x| acc + x)
0
[1, 2, 3]

-- 6
Try it

Notes: Tail-recursive, efficient for large lists.

See also: List.foldr, List.sum

List.foldr

(a -> b -> b) -> b -> [a] -> b

Right fold: apply function to each element and accumulator from right to left.

Example:
import List

List.foldr (|x acc| x :: acc)
[]
[1, 2, 3]

-- [1, 2, 3]
Try it

Notes: Useful for building lists, but not tail-recursive.

See also: List.foldl

List.sum

[Int] -> Int

Calculate the sum of all elements.

Example:
import List

List.sum [ 1
, 2
, 3
, 4
]

-- 10
Try it

Notes: Also works with [Float] -> Float.

See also: List.product, List.foldl

List.product

[Int] -> Int

Calculate the product of all elements.

Example:
import List

List.product [ 1
, 2
, 3
, 4
]

-- 24
Try it

Notes: Also works with [Float] -> Float. Returns 1 for empty list.

See also: List.sum

List.maximum

[comparable] -> Maybe comparable

Find the maximum element in a list.

Example:
import List

List.maximum [ 3
, 1
, 4
, 1
, 5
]
Try it

Notes: Works with Int, Float, String, Bool.

See also: List.minimum, Math.max

List.minimum

[comparable] -> Maybe comparable

Find the minimum element in a list.

Example:
import List

List.minimum [ 3
, 1
, 4
, 1
, 5
]
Try it

Notes: Works with Int, Float, String, Bool.

See also: List.maximum, Math.min

List.length

[a] -> Int

Get the number of elements in a list.

Example:
import List

List.length [1, 2, 3]

-- 3
Try it

See also: List.isEmpty

Accessing

List.tail

[a] -> [a]

Get all elements except the first. Returns empty list if input is empty.

Example:
import List

List.tail [1, 2, 3]
Try it

See also: List.head, List.init

List.last

[a] -> Maybe a

Get the last element of a list, or Nothing if empty.

Example:
import List

List.last [1, 2, 3]
Try it

See also: List.head, List.init

List.init

[a] -> Maybe [a]

Get all elements except the last, or Nothing if empty.

Example:
import List

List.init [1, 2, 3]
Try it

See also: List.tail, List.last

List.nth

Int -> [a] -> Maybe a

Get element at the given index (0-based), or Nothing if out of bounds.

Example:
import List

List.nth 1
["a", "b", "c"]
Try it

See also: List.head, List.last

Building

List.range

Int -> Int -> [Int]

Generate a list of integers from start to end (inclusive).

Example:
import List

List.range 1 5
Try it

Notes: Works in both ascending and descending order.

See also: List.repeat

List.repeat

Int -> a -> [a]

Create a list with n copies of a value.

Example:
import List

List.repeat 3 "x"

-- ["x", "x", "x"]
Try it

Notes: Returns empty list if n <= 0.

See also: List.range

List.singleton

a -> [a]

Create a list with a single element.

Example:
import List

List.singleton 42

-- [42]
Try it

See also: List.repeat

List.append

[a] -> [a] -> [a]

Concatenate two lists.

Example:
import List

List.append [1, 2]
[3, 4]

-- [1, 2, 3, 4]
Try it

See also: List.concat

List.concat

[[a]] -> [a]

Flatten a list of lists into a single list.

Example:
import List

List.concat [[1, 2], [3, 4]]

-- [1, 2, 3, 4]
Try it

See also: List.append

Searching

List.find

(a -> Bool) -> [a] -> Maybe a

Find the first element that satisfies the predicate.

Example:
import List

List.find (|x| x > 2)
[ 1
, 2
, 3
, 4
]

-- Just 3
Try it

See also: List.filter, List.any

List.any

(a -> Bool) -> [a] -> Bool

Check if any element satisfies the predicate.

Example:
import List

List.any (|x| x > 3)
[1, 2, 5]

-- True
Try it

Notes: Short-circuits on first match.

See also: List.all, List.find

List.all

(a -> Bool) -> [a] -> Bool

Check if all elements satisfy the predicate.

Example:
import List

List.all (|x| x > 0)
[1, 2, 3]

-- True
Try it

Notes: Short-circuits on first failure.

See also: List.any

List.member

a -> [a] -> Bool

Check if an element is in a list.

Example:
import List

List.member 2
[1, 2, 3]
Try it

See also: List.find, List.any

List.isEmpty

[a] -> Bool

Check if a list is empty.

Example:
import List

List.isEmpty []
Try it

See also: List.length

Ordering

List.sort

[comparable] -> [comparable]

Sort a list in ascending order.

Example:
import List

List.sort [ 3
, 1
, 4
, 1
, 5
]

-- [1, 1, 3, 4, 5]
Try it

Notes: Works with Int, Float, String, Bool. Uses stable sort.

See also: List.sortBy, List.reverse

List.sortBy

(a -> comparable) -> [a] -> [a]

Sort a list using a key function.

Example:
import List

List.sortBy (|x| 0 - x)
[3, 1, 4]
Try it

Notes: Uses stable sort.

See also: List.sort

List.reverse

[a] -> [a]

Reverse the order of elements in a list.

Example:
import List

List.reverse [1, 2, 3]

-- [3, 2, 1]
Try it

List.unique

[comparable] -> [comparable]

Remove duplicate elements from a list, preserving the order of first occurrences.

Example:
import List

List.unique [ 1
, 2
, 1
, 3
, 2
]

-- [1, 2, 3]
Try it

Notes: Works with Int, Float, String, Bool.

See also: List.sort

Slicing

List.take

Int -> [a] -> [a]

Take the first n elements from a list.

Example:
import List

List.take 2
[ 1
, 2
, 3
, 4
]

-- [1, 2]
Try it

Notes: Returns entire list if n > length.

See also: List.drop

List.drop

Int -> [a] -> [a]

Drop the first n elements from a list.

Example:
import List

List.drop 2
[ 1
, 2
, 3
, 4
]

-- [3, 4]
Try it

Notes: Returns empty list if n >= length.

See also: List.take

List.partition

(a -> Bool) -> [a] -> ([a], [a])

Split a list into two lists based on a predicate. Returns (matching, not matching).

Example:
import List

List.partition (|x| x > 2)
[ 1
, 2
, 3
, 4
]

-- ([3, 4], [1, 2])
Try it

See also: List.filter