feat: add List.rotateLeft and List.rotateRight

This commit is contained in:
Leonardo de Moura 2021-03-12 17:09:22 -08:00
parent 7627458aac
commit 091fadbf64

View file

@ -10,10 +10,8 @@ import Init.Util
universes u
namespace List
/- 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`. -/
variable {α : Type u}
/- 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`. -/
def get! [Inhabited α] : Nat → List αα
| 0, a::as => a
@ -69,4 +67,24 @@ def getLastD : List ααα
| [], a₀ => a₀
| a::as, _ => getLast (a::as) (fun h => List.noConfusion h)
def rotateLeft (xs : List α) (n : Nat := 1) : List α :=
let len := xs.length
if len ≤ 1 then
xs
else
let n := n % len
let b := xs.take n
let e := xs.drop n
e ++ b
def rotateRight (xs : List α) (n : Nat := 1) : List α :=
let len := xs.length
if len ≤ 1 then
xs
else
let n := len - n % len
let b := xs.take n
let e := xs.drop n
e ++ b
end List