diff options
| author | Andreas Grois <andi@grois.info> | 2023-12-03 21:13:47 +0100 |
|---|---|---|
| committer | Andreas Grois <andi@grois.info> | 2023-12-03 21:13:47 +0100 |
| commit | 0cdb5a2449c996e54a0c777dd807785d3016fd66 (patch) | |
| tree | 40f1ffe9a46fe2d342c37204361ee1d2c897110a /Common | |
| parent | d09a6869fea6f0d79c57e4ff756e21e03d395d52 (diff) | |
Day 3
Diffstat (limited to 'Common')
| -rw-r--r-- | Common/Char.lean | 4 | ||||
| -rw-r--r-- | Common/List.lean | 26 |
2 files changed, 29 insertions, 1 deletions
diff --git a/Common/Char.lean b/Common/Char.lean new file mode 100644 index 0000000..9c727eb --- /dev/null +++ b/Common/Char.lean @@ -0,0 +1,4 @@ +namespace Char + +def asDigit (d : Char) (ok : Char.isDigit d = true) : Nat := + d.toNat - 48 diff --git a/Common/List.lean b/Common/List.lean index 4076c87..1a76c3e 100644 --- a/Common/List.lean +++ b/Common/List.lean @@ -1,3 +1,5 @@ +namespace List + theorem listFilterSmallerOrEqualList (l : List α) (p : α → Bool) : l.length ≥ (l.filter p).length := by induction l with | nil => simp[List.filter] @@ -10,7 +12,6 @@ theorem listFilterSmallerOrEqualList (l : List α) (p : α → Bool) : l.length assumption | true => simp_arith[hi] - def quicksort {α : Type} [Ord α] : List α → List α | [] => [] | a :: as => @@ -22,3 +23,26 @@ def quicksort {α : Type} [Ord α] : List α → List α let biggers := as.filter largerEqualPred (quicksort smallers) ++ [a] ++ (quicksort biggers) termination_by quicksort l => l.length + +/-- Maps a List to another list, but keeps state. + The output list is a list of the state, and **has** the initial state + prepended! + -/ +def scan {α σ : Type} (step : σ → α → σ) (init : σ): List α → List σ +| [] => [init] +| a :: as => + let next := step init a + init :: scan step next as + +/-- Removes repeated entries. [1,2,2,1] becomes [1,2,1]-/ +def dedup {α : Type} [BEq α] (input : List α) : List α := + let rec helper : List α → α → List α := λ + | [], _ => [] + | a :: as, b => + if a == b then + helper as a + else + a :: helper as a + match input with + | [] => [] + | a :: as => a :: helper as a |
