mirror of
https://github.com/anoma/juvix.git
synced 2025-01-05 22:46:08 +03:00
43d114f9b1
Progress for #1742 * Remove putStr and putStrLn * Remove named Nats (one, two, ...)
40 lines
1.1 KiB
Plaintext
40 lines
1.1 KiB
Plaintext
module QuickSort;
|
||
|
||
open import Stdlib.Prelude;
|
||
open import Stdlib.Data.Nat.Ord;
|
||
open import Stdlib.Data.Ord;
|
||
|
||
qsHelper : {A : Type} → A → List A × List A → List A;
|
||
qsHelper a (l , r) := l ++ (a :: nil) ++ r;
|
||
|
||
terminating
|
||
quickSort : {A : Type} → (A → A → Ordering) → List A → List A;
|
||
quickSort _ nil := nil;
|
||
quickSort _ (x :: nil) := x :: nil;
|
||
quickSort cmp (x :: xs) := qsHelper x
|
||
(both (quickSort cmp) (partition (isGT ∘ cmp x) xs));
|
||
|
||
uniq : {A : Type} -> (A -> A -> Ordering) -> List A -> List A;
|
||
uniq _ nil := nil;
|
||
uniq _ y@(x :: nil) := y;
|
||
uniq cmp (x :: x' :: xs) := if (isEQ (cmp x x')) (uniq cmp (x' :: xs)) (x :: (uniq cmp (x' :: xs)));
|
||
|
||
gen : Nat -> (Nat -> Nat) -> List Nat -> List Nat;
|
||
gen zero _ acc := acc;
|
||
gen n@(suc n') f acc := gen n' f (f n :: acc);
|
||
|
||
gen2 : Nat -> Nat -> List (List Nat) -> List (List Nat);
|
||
gen2 _ zero acc := acc;
|
||
gen2 m n@(suc n') acc := gen2 m n' ((gen m ((+) n) nil) :: acc);
|
||
|
||
three : Nat;
|
||
three := suc (suc (suc zero));
|
||
|
||
four : Nat;
|
||
four := suc three;
|
||
|
||
main : List Nat;
|
||
main := uniq compare (quickSort compare (flatten (gen2 three four nil)));
|
||
|
||
end;
|