We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent b48510b commit 22dbec7Copy full SHA for 22dbec7
Fad/Chapter6.lean
@@ -111,13 +111,20 @@ def median (xs : List a) : a :=
111
select₀ k xs
112
113
114
-partial def group (n : Nat) (xs : List a) : List (List a) :=
115
- match xs with
116
- | [] => []
117
- | xs =>
118
- let p := xs.splitAt n
119
- p.1 :: (group n p.2)
120
-
+def group (n : Nat) (xs : List a) : List (List a) :=
+ if h₁ : n = 0 then []
+ else if h₂ : xs = [] then []
+ else
+ let p := xs.splitAt n
+ have : xs.length - n < xs.length := by
+ induction xs with
121
+ | nil => simp at *
122
+ | cons b bs ih =>
123
+ simp ; omega
124
+ p.1 :: (group n p.2)
125
+ termination_by xs.length
126
+
127
+-- #eval group 5 (List.range' 1 12)
128
129
/- `qsort₁` or `qsort` ? -/
130
def medians : List a → List a :=
0 commit comments