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 n
elements from the tail end of a list.
Equations
- List.rdrop l n = List.take (List.length l - n) l
Instances For
Take n
elements from the tail end of a list.
Equations
- List.rtake l n = List.drop (List.length l - n) l
Instances For
Drop elements from the tail end of a list that satisfy p : α → Bool
.
Implemented naively via List.reverse
Equations
- List.rdropWhile p l = List.reverse (List.dropWhile p (List.reverse l))
Instances For
Take elements from the tail end of a list that satisfy p : α → Bool
.
Implemented naively via List.reverse
Equations
- List.rtakeWhile p l = List.reverse (List.takeWhile p (List.reverse l))