Dropping or taking from lists on the right #
Taking or removing element from the tail end of a list
Main definitions #
rdrop n
: dropn : ℕ
elements from the tailrtake n
: taken : ℕ
elements from the tailrdropWhile p
: remove all the elements from the tail of a list until it finds the first element for whichp : α → Bool
returns false. This element and everything before is returned.rtakeWhile p
: Returns the longest terminal segment of a list for whichp : α → Bool
returns true.
Implementation detail #
The two predicate-based methods operate by performing the regular "from-left" operation on
List.reverse
, followed by another List.reverse
, so they are not the most performant.
The other two rely on List.length l
so they still traverse the list twice. One could construct
another function that takes a L : ℕ
and use L - n
. Under a proof condition that
L = l.length
, the function would do the right thing.
Drop elements from the tail end of a list that satisfy p : α → Bool
.
Implemented naively via List.reverse
Equations
- List.rdropWhile p l = (List.dropWhile p l.reverse).reverse
Instances For
theorem
List.rdropWhile_concat
{α : Type u_1}
(p : α → Bool)
(l : List α)
(x : α)
:
List.rdropWhile p (l ++ [x]) = if p x = true then List.rdropWhile p l else l ++ [x]
@[simp]
theorem
List.rdropWhile_concat_pos
{α : Type u_1}
(p : α → Bool)
(l : List α)
(x : α)
(h : p x = true)
:
List.rdropWhile p (l ++ [x]) = List.rdropWhile p l
theorem
List.rdropWhile_singleton
{α : Type u_1}
(p : α → Bool)
(x : α)
:
List.rdropWhile p [x] = if p x = true then [] else [x]
theorem
List.rdropWhile_last_not
{α : Type u_1}
(p : α → Bool)
(l : List α)
(hl : List.rdropWhile p l ≠ [])
:
¬p ((List.rdropWhile p l).getLast hl) = true
theorem
List.rdropWhile_prefix
{α : Type u_1}
(p : α → Bool)
(l : List α)
:
List.rdropWhile p l <+: l
theorem
List.dropWhile_idempotent
{α : Type u_1}
(p : α → Bool)
(l : List α)
:
List.dropWhile p (List.dropWhile p l) = List.dropWhile p l
theorem
List.rdropWhile_idempotent
{α : Type u_1}
(p : α → Bool)
(l : List α)
:
List.rdropWhile p (List.rdropWhile p l) = List.rdropWhile p l
Take elements from the tail end of a list that satisfy p : α → Bool
.
Implemented naively via List.reverse
Equations
- List.rtakeWhile p l = (List.takeWhile p l.reverse).reverse
Instances For
theorem
List.rtakeWhile_concat
{α : Type u_1}
(p : α → Bool)
(l : List α)
(x : α)
:
List.rtakeWhile p (l ++ [x]) = if p x = true then List.rtakeWhile p l ++ [x] else []
@[simp]
theorem
List.rtakeWhile_concat_pos
{α : Type u_1}
(p : α → Bool)
(l : List α)
(x : α)
(h : p x = true)
:
List.rtakeWhile p (l ++ [x]) = List.rtakeWhile p l ++ [x]
theorem
List.rtakeWhile_suffix
{α : Type u_1}
(p : α → Bool)
(l : List α)
:
List.rtakeWhile p l <:+ l
theorem
List.mem_rtakeWhile_imp
{α : Type u_1}
{p : α → Bool}
{l : List α}
{x : α}
(hx : x ∈ List.rtakeWhile p l)
:
theorem
List.rtakeWhile_idempotent
{α : Type u_1}
(p : α → Bool)
(l : List α)
:
List.rtakeWhile p (List.rtakeWhile p l) = List.rtakeWhile p l