---
title: Std.List
---

Operations on linked lists: transforming, searching, sorting, and more.

## Functions

### head

```saga
fun head : List a -> a
```

Returns the first element of a list. Panics on empty list.

### tail

```saga
fun tail : List a -> List a
```

Returns all elements after the first. Panics on empty list.

### last

```saga
fun last : List a -> a
```

Returns the last element of a list. Panics on empty list.

### length

```saga
fun length : (xs: List a) -> Int
```

Returns the number of elements in a list.

### is_empty

```saga
fun is_empty : List a -> Bool
```

Returns True if the list has no elements.

### map

```saga
fun map : (f: a -> b needs {..e}) -> (xs: List a) -> List b needs {..e}
```

Applies a function to every element, returning a new list of results.

### filter

```saga
fun filter : (pred: a -> Bool needs {..e}) -> (xs: List a) -> List a needs {..e}
```

Returns a list of elements for which the predicate returns True.

### foldl

```saga
fun foldl : (f: b -> a -> b needs {..e}) -> (acc: b) -> (xs: List a) -> b needs {..e}
```

Left fold. Applies f to the accumulator and each element, left to right.

### foldr

```saga
fun foldr : (f: a -> b -> b needs {..e}) -> (acc: b) -> (xs: List a) -> b needs {..e}
```

Right fold. Applies f to each element and the accumulator, right to left.

### reverse

```saga
fun reverse : (xs: List a) -> List a
```

Returns a list with elements in reverse order.

### append

```saga
fun append : (xs: List a) -> (ys: List a) -> List a
```

Concatenates two lists.

### flat_map

```saga
fun flat_map : (f: a -> List b needs {..e}) -> (xs: List a) -> List b needs {..e}
```

Maps a function over a list and flattens the results by one level.

### filter_map

```saga
fun filter_map : (f: a -> Maybe b needs {..e}) -> (xs: List a) -> List b needs {..e}
```

Maps a function over a list and keeps only the Just results.

### take

```saga
fun take : (n: Int) -> (xs: List a) -> List a
```

Returns the first n elements of a list.

### drop

```saga
fun drop : (n: Int) -> (xs: List a) -> List a
```

Drops the first n elements of a list and returns the rest.

### zip

```saga
fun zip : (xs: List a) -> (ys: List b) -> List (a, b)
```

Pairs up elements from two lists. Stops at the shorter list.

### any

```saga
fun any : (pred: a -> Bool needs {..e}) -> (xs: List a) -> Bool needs {..e}
```

Returns True if any element satisfies the predicate.

### all

```saga
fun all : (pred: a -> Bool needs {..e}) -> (xs: List a) -> Bool needs {..e}
```

Returns True if all elements satisfy the predicate.

### range

```saga
fun range : (start: Int) -> (end: Int) -> List Int
```

Creates a list of integers from start to end, inclusive.

### sort

```saga
fun sort : (xs: List a) -> List a where {a: Ord}
```

Sorts a list in ascending order using the Ord instance.

### sort_with

```saga
fun sort_with : (cmp: a -> a -> Ordering) -> (xs: List a) -> List a
```

Sorts a list using a custom comparison function.

### sort_by

```saga
fun sort_by : (key: a -> b) -> (xs: List a) -> List a
```

Sorts a list by comparing the results of a key function.

### find

```saga
fun find : (pred: a -> Bool needs {..e}) -> (xs: List a) -> Maybe a needs {..e}
```

Returns the first element matching the predicate, or Nothing.

### contains

```saga
fun contains : (elem: a) -> (xs: List a) -> Bool where {a: Eq}
```

Returns True if the element is in the list.

### nth

```saga
fun nth : (n: Int) -> (xs: List a) -> Maybe a
```

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

### zip_with

```saga
fun zip_with : (f: a -> b -> c needs {..e}) -> (xs: List a) -> (ys: List b) -> List c needs {..e}
```

Combines two lists element-wise using a function. Stops at the shorter list.

### unzip

```saga
fun unzip : (xs: List (a, b)) -> (List a, List b)
```

Transforms a list of pairs into a pair of lists.

### partition

```saga
fun partition : (pred: a -> Bool needs {..e}) -> (xs: List a) -> (List a, List a) needs {..e}
```

Splits a list into two: elements that satisfy the predicate and those that don't.

### enumerate

```saga
fun enumerate : (xs: List a) -> List (Int, a)
```

Pairs each element with its 0-based index.

### intersperse

```saga
fun intersperse : (sep: a) -> (xs: List a) -> List a
```

Inserts a separator between each element of a list.

### unique

```saga
fun unique : (xs: List a) -> List a where {a: Eq}
```

Removes duplicate elements, preserving the first occurrence of each.

### concat

```saga
fun concat : (xss: List (List a)) -> List a
```

Flattens a list of lists into a single list.

### iter

```saga
fun iter : (f: a -> Unit needs {..e}) -> (xs: List a) -> Unit needs {..e}
```

Applies a side-effecting function to each element.

### scan

```saga
fun scan : (f: b -> a -> b needs {..e}) -> (acc: b) -> (xs: List a) -> List b needs {..e}
```

Like foldl, but returns a list of all intermediate accumulator values.

### chunks

```saga
fun chunks : (n: Int) -> (xs: List a) -> List (List a)
```

Splits a list into chunks of the given size. The last chunk may be smaller.

### window

```saga
fun window : (n: Int) -> (xs: List a) -> List (List a)
```

Returns a sliding window of the given size over the list.

