The following functions can't be defined at Init.Data.List.Basic
, because they depend on Init.Util
,
and Init.Util
depends on Init.Data.List.Basic
.
Returns the first element in the list.
If the list is empty, this function panics when executed, and returns default
.
See head
and headD
for safer alternatives.
Equations
- x.head! = match x with | [] => panicWithPosWithDecl "Init.Data.List.BasicAux" "List.head!" 62 12 "empty list" | a :: tail => a
Instances For
Drops the first element of the list.
If the list is empty, this function panics when executed, and returns the empty list.
See tail
and tailD
for safer alternatives.
Equations
- x.tail! = match x with | [] => panicWithPosWithDecl "Init.Data.List.BasicAux" "List.tail!" 98 13 "empty list" | head :: as => as
Instances For
Returns the last element in the list.
If the list is empty, this function panics when executed, and returns default
.
See getLast
and getLastD
for safer alternatives.
Equations
- x.getLast! = match x with | [] => panicWithPosWithDecl "Init.Data.List.BasicAux" "List.getLast!" 137 13 "empty list" | a :: as => (a :: as).getLast ⋯
Instances For
O(n)
. Rotates the elements of xs
to the left such that the element at
xs[i]
rotates to xs[(i - n) % l.length]
.
rotateLeft [1, 2, 3, 4, 5] 3 = [4, 5, 1, 2, 3]
rotateLeft [1, 2, 3, 4, 5] 5 = [1, 2, 3, 4, 5]
rotateLeft [1, 2, 3, 4, 5] = [2, 3, 4, 5, 1]
Equations
Instances For
O(n)
. Rotates the elements of xs
to the right such that the element at
xs[i]
rotates to xs[(i + n) % l.length]
.
rotateRight [1, 2, 3, 4, 5] 3 = [3, 4, 5, 1, 2]
rotateRight [1, 2, 3, 4, 5] 5 = [1, 2, 3, 4, 5]
rotateRight [1, 2, 3, 4, 5] = [5, 1, 2, 3, 4]
Equations
Instances For
This tactic, added to the decreasing_trivial
toolbox, proves that
sizeOf a < sizeOf as
when a ∈ as
, which is useful for well founded recursions
over a nested inductive like inductive T | mk : List T → T
.
Equations
- List.tacticSizeOf_list_dec = Lean.ParserDescr.node `List.tacticSizeOf_list_dec 1024 (Lean.ParserDescr.nonReservedSymbol "sizeOf_list_dec" false)
Instances For
Monomorphic List.mapM
. The internal implementation uses pointer equality, and does not allocate a new list
if the result of each f a
is a pointer equal value a
.
Equations
Instances For
Monadic generalization of List.partition
.
This uses Array.toList
and which isn't imported by Init.Data.List.Basic
.
def posOrNeg (x : Int) : Except String Bool :=
if x > 0 then pure true
else if x < 0 then pure false
else throw "Zero is not positive or negative"
partitionM posOrNeg [-1, 2, 3] = Except.ok ([2, 3], [-1])
partitionM posOrNeg [0, 2, 3] = Except.error "Zero is not positive or negative"
Equations
- List.partitionM p l = List.partitionM.go p l #[] #[]
Instances For
Auxiliary for partitionM
:
partitionM.go p l acc₁ acc₂
returns (acc₁.toList ++ left, acc₂.toList ++ right)
if partitionM p l
returns (left, right)
.
Equations
- List.partitionM.go p [] x✝ x = pure (x✝.toList, x.toList)
- List.partitionM.go p (x_3 :: xs) x✝ x = do let __do_lift ← p x_3 if __do_lift = true then List.partitionM.go p xs (x✝.push x_3) x else List.partitionM.go p xs x✝ (x.push x_3)
Instances For
Given a function f : α → β ⊕ γ
, partitionMap f l
maps the list by f
whilst partitioning the result it into a pair of lists, List β × List γ
,
partitioning the .inl _
into the left list, and the .inr _
into the right List.
partitionMap (id : Nat ⊕ Nat → Nat ⊕ Nat) [inl 0, inr 1, inl 2] = ([0, 2], [1])
Equations
- List.partitionMap f l = List.partitionMap.go f l #[] #[]
Instances For
Auxiliary for partitionMap
:
partitionMap.go f l acc₁ acc₂ = (acc₁.toList ++ left, acc₂.toList ++ right)
if partitionMap f l = (left, right)
.
Equations
- List.partitionMap.go f [] x✝ x = (x✝.toList, x.toList)
- List.partitionMap.go f (x_3 :: xs) x✝ x = match f x_3 with | Sum.inl a => List.partitionMap.go f xs (x✝.push a) x | Sum.inr b => List.partitionMap.go f xs x✝ (x.push b)