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)number—IntorFloat(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.
import List
List.map (|x| x * 2)
[1, 2, 3]
-- [2, 4, 6]Try itSee also: List.filter, List.foldl
List.filter
(a -> Bool) -> [a] -> [a]
Keep only elements that satisfy the predicate.
import List
List.filter (|x| x > 2)
[ 1
, 2
, 3
, 4
]
-- [3, 4]Try itSee also: List.map, List.find, List.partition
List.zip
[a] -> [b] -> [(a, b)]
Pair up elements from two lists into a list of tuples.
import List
List.zip [1, 2]
["a", "b"]
-- [(1, "a"), (2, "b")]Try itNotes: 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.
import List
List.zipWith (|a b| a + b)
[1, 2]
[10, 20]
-- [11, 22]Try itNotes: Stops at the shorter list.
List.intersperse
a -> [a] -> [a]
Insert a value between each element of a list.
import List
List.intersperse 0
[1, 2, 3]
-- [1, 0, 2, 0, 3]Try itSee 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.
import List
List.foldl (|acc x| acc + x)
0
[1, 2, 3]
-- 6Try itNotes: 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.
import List
List.foldr (|x acc| x :: acc)
[]
[1, 2, 3]
-- [1, 2, 3]Try itNotes: Useful for building lists, but not tail-recursive.
See also: List.foldl
List.sum
[Int] -> Int
Calculate the sum of all elements.
import List
List.sum [ 1
, 2
, 3
, 4
]
-- 10Try itNotes: Also works with [Float] -> Float.
See also: List.product, List.foldl
List.product
[Int] -> Int
Calculate the product of all elements.
import List
List.product [ 1
, 2
, 3
, 4
]
-- 24Try itNotes: 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.
import List
List.maximum [ 3
, 1
, 4
, 1
, 5
]Try itNotes: Works with Int, Float, String, Bool.
See also: List.minimum, Math.max
List.minimum
[comparable] -> Maybe comparable
Find the minimum element in a list.
import List
List.minimum [ 3
, 1
, 4
, 1
, 5
]Try itNotes: Works with Int, Float, String, Bool.
See also: List.maximum, Math.min
List.length
[a] -> Int
Get the number of elements in a list.
import List
List.length [1, 2, 3]
-- 3Try itSee also: List.isEmpty
Accessing
List.head
[a] -> Maybe a
Get the first element of a list, or Nothing if empty.
import List
List.head [1, 2, 3]Try itList.tail
[a] -> [a]
Get all elements except the first. Returns empty list if input is empty.
import List
List.tail [1, 2, 3]Try itList.last
[a] -> Maybe a
Get the last element of a list, or Nothing if empty.
import List
List.last [1, 2, 3]Try itList.init
[a] -> Maybe [a]
Get all elements except the last, or Nothing if empty.
import List
List.init [1, 2, 3]Try itList.nth
Int -> [a] -> Maybe a
Get element at the given index (0-based), or Nothing if out of bounds.
import List
List.nth 1
["a", "b", "c"]Try itBuilding
List.range
Int -> Int -> [Int]
Generate a list of integers from start to end (inclusive).
import List
List.range 1 5Try itNotes: 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.
import List
List.repeat 3 "x"
-- ["x", "x", "x"]Try itNotes: Returns empty list if n <= 0.
See also: List.range
List.singleton
a -> [a]
Create a list with a single element.
import List
List.singleton 42
-- [42]Try itSee also: List.repeat
List.append
[a] -> [a] -> [a]
Concatenate two lists.
import List
List.append [1, 2]
[3, 4]
-- [1, 2, 3, 4]Try itSee also: List.concat
List.concat
[[a]] -> [a]
Flatten a list of lists into a single list.
import List
List.concat [[1, 2], [3, 4]]
-- [1, 2, 3, 4]Try itSee also: List.append
Searching
List.find
(a -> Bool) -> [a] -> Maybe a
Find the first element that satisfies the predicate.
import List
List.find (|x| x > 2)
[ 1
, 2
, 3
, 4
]
-- Just 3Try itSee also: List.filter, List.any
List.any
(a -> Bool) -> [a] -> Bool
Check if any element satisfies the predicate.
import List
List.any (|x| x > 3)
[1, 2, 5]
-- TrueTry itNotes: Short-circuits on first match.
List.all
(a -> Bool) -> [a] -> Bool
Check if all elements satisfy the predicate.
import List
List.all (|x| x > 0)
[1, 2, 3]
-- TrueTry itNotes: Short-circuits on first failure.
See also: List.any
List.member
a -> [a] -> Bool
Check if an element is in a list.
import List
List.member 2
[1, 2, 3]Try itList.isEmpty
[a] -> Bool
Check if a list is empty.
import List
List.isEmpty []Try itSee also: List.length
Ordering
List.sort
[comparable] -> [comparable]
Sort a list in ascending order.
import List
List.sort [ 3
, 1
, 4
, 1
, 5
]
-- [1, 1, 3, 4, 5]Try itNotes: 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.
import List
List.sortBy (|x| 0 - x)
[3, 1, 4]Try itNotes: Uses stable sort.
See also: List.sort
List.reverse
[a] -> [a]
Reverse the order of elements in a list.
import List
List.reverse [1, 2, 3]
-- [3, 2, 1]Try itList.unique
[comparable] -> [comparable]
Remove duplicate elements from a list, preserving the order of first occurrences.
import List
List.unique [ 1
, 2
, 1
, 3
, 2
]
-- [1, 2, 3]Try itNotes: Works with Int, Float, String, Bool.
See also: List.sort
Slicing
List.take
Int -> [a] -> [a]
Take the first n elements from a list.
import List
List.take 2
[ 1
, 2
, 3
, 4
]
-- [1, 2]Try itNotes: Returns entire list if n > length.
See also: List.drop
List.drop
Int -> [a] -> [a]
Drop the first n elements from a list.
import List
List.drop 2
[ 1
, 2
, 3
, 4
]
-- [3, 4]Try itNotes: 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).
import List
List.partition (|x| x > 2)
[ 1
, 2
, 3
, 4
]
-- ([3, 4], [1, 2])Try itSee also: List.filter