---
title: "first"
url: https://una-lang.org/en/docs/std/prelude/first/
release: v0.4.0
edition: "0.1 and later"
source: https://github.com/una-lang/una/blob/v0.4.0/docs/std/listing.json
---

# first

A function of the prelude, in scope in every module. Edition 0.1 and later.

```una
pub first[T](xs: List[T]) -> Option[T]
  ensures (result is None) == (xs.len == 0)
```

`Some` of the element at position 0, and `None` exactly when `xs` is empty (`spec/14 §1`).

## Parameters

- `xs`: the list

## Returns

`Option[T]`: `Some` of the element at position `0`\, and `None` exactly when `xs` is empty.

## Use instead

- [`first_where`](https://una-lang.org/en/docs/std/prelude/first_where/) for the first element that meets a condition

## See also

[`at`](https://una-lang.org/en/docs/std/prelude/at/), [`or_else`](https://una-lang.org/en/docs/std/prelude/or_else/)

## Cost

Time `O(1) index` (ADR 0107).

## Example

`src/shop.una`

```una
module shop

--- The first item of a cart, if any.
describe(names: List[Text]) -> Text
=
  when names.first()
    is Some(name) -> "first: {name}"
    is None -> "the cart is empty"

pub main() -> Unit
  uses @console: Console[print]
=
  @console.print(describe(["lamp", "desk"]))
  @console.print(describe([]))
```

`una run .` prints:

```text
first: lamp
the cart is empty
```

## Specification

- [`spec/14 §1`](https://una-lang.org/en/docs/spec/14-standard-library/#s1)

Generated from the standard modules of una v0.4.0 ([`docs/std/listing.json`](https://github.com/una-lang/una/blob/5fec93d88932b333feec25e589355a2cb34c37b5/docs/std/listing.json), which is not part of the specification). The specification governs: [`spec/14 §1`](https://una-lang.org/en/docs/spec/14-standard-library/#s1).
