query
On this page

tail

lib.strings.tail

Primop
Docs pulled from | This Revision | 18 minutes ago


Nix manual

Takes 1 arguments

list

Return the list without its first item; abort evaluation if the argument isn’t a list or is an empty list.

Warning

This function should generally be avoided since it's inefficient: unlike Haskell's tail, it takes O(n) time, so recursing over a list by repeatedly calling tail takes O(n^2) time.

Time Complexity

O(n) where n = list length (copies n-1 elements)

Noogle detected

Aliases

Detected Type
tail :: [a] -> [a]

Implementation

This function is implemented in c++ and is part of the native nix runtime.

src/libexpr/primops.cc:3779

static void prim_tail(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
    state.forceList(*args[0], pos, "while evaluating the first argument passed to 'builtins.tail'");
    if (args[0]->listSize() == 0)
        state.error<EvalError>("'builtins.tail' called on an empty list").atPos(pos).debugThrow();

    auto list = state.buildList(args[0]->listSize() - 1);
    for (const auto & [n, v] : enumerate(list))
        v = args[0]->listView()[n + 1];
    v.mkList(list);
}