> For the complete documentation index, see [llms.txt](https://xylo-lang.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xylo-lang.gitbook.io/docs/builtin-functions/list.md).

# List

### List Operators

#### range from to

Generates a list of all numbers between `from` (inclusive) and `to` (exclusive). List elements can be integer or float types.

Equivalent to the `(..)` binary operator.

Example:

```ocaml
one_two_three = range 1 4
one_two_three = (..) 1 4
one_two_three = 1 .. 4
```

#### rangei from to

Returns a list of all numbers between `from` (inclusive) and `to` (inclusive). List elements can be integer or float types.

Equivalent to the `(..=)` binary operator.

Example:

```ocaml
one_two_three = rangei 1 3
one_two_three = (..=) 1 3
one_two_three = 1 ..= 3
```

#### concat x y

Returns a list of length 2 containing `x` as the first element and `y` as the second element. `x` and `y` must be of the same type.

Equivalent to the `(++)` binary operator.

Example:

```ocaml
one_two = concat 1 2
one_two = (++) 1 2
one_two = 1 ++ 2
```

#### prepend value list

Return a new list with `value` added to the start of `list`. `value` must be the same type as the elements of `list`.

Equivalent to the `(+>)` binary operator.

Example:

```ocaml
one_two_three = prepend 1 [2, 3]
one_two_three = (+>) 1 [2, 3]
one_two_three = 1 +> [2, 3]
```

#### append list value

Return a new list with `value` added to the end of `list`. `value` must be the same type as the elements of `list`.

Equivalent to the `(<+)` binary operator.

Example:

```ocaml
one_two_three = append [1, 2] 3
one_two_three = (<+) [1, 2] 3
one_two_three = [1, 2] <+ 3
```

### Query Functions

#### nth index list

Returns the element at `index` of `list`. `index` must be of the integer type.

Example:

```ocaml
two = nth 1 [1, 2, 3]
```

#### length list

Returns the number of elements in `list`.

Example:

```ocaml
three = length [1, 2, 3]
```

#### is\_empty list

Returns true if length of `list`  is zero, false otherwise.

Example:

```ocaml
true_ = is_empty []
```

#### contains value list

Returns true if `value` is equal to an element in `list`, false otherwise.

Example:

```ocaml
true_ = contains 3 [1, 2, 3]
```

#### index\_of value list

Returns the index of first element in `list` that is equal to `value`, -1 if not found.

Example:

```ocaml
one = index_of 2 [1, 2, 3, 2]
```

### Modification Functions

#### set index value list

Returns a new list where element of `list` at `index`  is replaced with `value`. `index` must be of the integer type and `value` must be the same type as elements of `list`.

Example:

```ocaml
one_two_three = set 1 2 [1, 3, 3]
```

#### head list

Returns the first element of `list`.

Example:

```ocaml
one = head [1, 2, 3]
```

#### tail list

Returns a new list containing all elements of `list` except the first one.

Example:

```ocaml
two_three = tail [1, 2, 3]
```

#### init list

Returns a new list containing all elements of `list` except the last one.

Example:

```ocaml
one_two = init [1, 2, 3]
```

#### last list

Returns the last element of `list`.

Example:

```ocaml
three = last [1, 2, 3]
```

#### take count list

Returns a new list containing the first `count` elements of `list`, or the last elements if `count` is negative. `count` must be of the integer type.

Example:

```ocaml
one_two_three = take 3 [1, 2, 3, 4, 5]
three_four_five = take -3 [1, 2, 3, 4, 5]
```

#### drop count list

Returns a new list excluding the first `count` elements of `list`, or the last elements if `count` is negative. `count` must be of the integer type.

Example:

```ocaml
three_four_five = drop 2 [1, 2, 3, 4, 5]
one_two_three = drop -2 [1, 2, 3, 4, 5]
```

#### slice start end list

Returns a new list containing elements from `list` between the `start` and `end` indices. `start` and `end` must be of the integer type.

Example:

```ocaml
two_three = slice 1 3 [1, 2, 3, 4, 5]
```

#### split index list

Divides `list` into two parts at `index`, returning a two-dimensional list containing the two resulting sublists. `index` must be of the integer type.

Example:

```ocaml
true_ = [[1, 2], [3, 4]] == split 2 [1, 2, 3, 4]
```

### Transformation Functions

#### reverse list

Returns a new list with the elements of `list` in reverse order.

Example:

```ocaml
three_two_one = reverse [1, 2, 3]
```

#### unique list

Returns a new list containing only the distinct elements of `list`, removing duplicates.

Example:

```ocaml
one_two_three = unique [1, 2, 1, 2, 3, 3, 3]
```

#### sort list

Returns a new list with the elements of `list` sorted in ascending order.

Example:

```ocaml
one_two_three = sort [1, 3, 2]
```

#### flatten list

Returns a new list by concatenating all nested lists within `list` into a single, one-dimensional list.

Example:

```ocaml
one_two_three = flatten [[1, 2], [3]]
```

#### intersperse value list

Returns a new list with a specified value inserted between each element of `list`. `value` must be of the same type of the elements of `list`.

Example:

```ocaml
one_four_two_four_three = intersperse 4 [1, 2, 3]
```

### Reduction Functions

#### min\_of list

Returns the smallest element of `list`. Elements of `list` must be of the integer or float types.

Example:

```ocaml
one = min_of [2, 1, 3]
```

#### max\_of list

Returns the largest element of `list`. Elements of `list` must be of the integer or float types.

Example:

```ocaml
three = max_of [1, 3, 2]
```

#### sum list

Returns the total sum of all elements in `list`. Elements of `list` must be of the integer, float, or complex types.

Example:

```ocaml
ten = sum [1, 2, 3, 4]
```

#### product list

Returns the total product of all elements in `list`. Elements of `list` must be of the integer, float, or complex types.

Example:

```ocaml
twenty_four = product [1, 2, 3, 4]
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xylo-lang.gitbook.io/docs/builtin-functions/list.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
