Skip to content

List.rev has quadratic complexity #19839

Closed
@MathisBD

Description

@MathisBD

Is your feature request related to a problem?

The definition of List.rev in the standard library is quadratic :

Fixpoint rev (l:list A) : list A :=
    match l with
      | [] => []
      | x :: l' => rev l' ++ [x]
    end.

This is very bad for such a common function.

Proposed solution

It should instead use an accumulator to obtain linear complexity :

Fixpoint rev (l : list A) :=
  let fix aux l acc :=
    match l with
    | [] => acc
    | x :: l => aux l (x :: acc)
    end
  in aux l []

Alternative solutions

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: wishFeature or enhancement requests.needs: triageThe validity of this issue needs to be checked, or the issue itself updated.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions