Skip to content

Commit 22dbec7

Browse files
committed
solves #36
1 parent b48510b commit 22dbec7

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

Fad/Chapter6.lean

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,20 @@ def median (xs : List a) : a :=
111111
select₀ k xs
112112

113113

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-
114+
def group (n : Nat) (xs : List a) : List (List a) :=
115+
if h₁ : n = 0 then []
116+
else if h₂ : xs = [] then []
117+
else
118+
let p := xs.splitAt n
119+
have : xs.length - n < xs.length := by
120+
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)
121128

122129
/- `qsort₁` or `qsort` ? -/
123130
def medians : List a → List a :=

0 commit comments

Comments
 (0)