Sort.agda 724 B

123456789101112131415161718192021222324252627282930313233
  1. module _ where
  2. open import Common.Prelude renaming (_∸_ to _-_) -- work-around for #1855
  3. _<_ : Nat → Nat → Bool
  4. a < b with b - a
  5. ... | zero = false
  6. ... | suc _ = true
  7. insert : Nat → List Nat → List Nat
  8. insert x [] = x ∷ []
  9. insert x (y ∷ xs) = if x < y then x ∷ y ∷ xs else (y ∷ insert x xs)
  10. sort : List Nat → List Nat
  11. sort [] = []
  12. sort (x ∷ xs) = insert x (sort xs)
  13. downFrom : Nat → List Nat
  14. downFrom zero = []
  15. downFrom (suc n) = n ∷ downFrom n
  16. mapM! : {A : Set} → (A → IO Unit) → List A → IO Unit
  17. mapM! f [] = return unit
  18. mapM! f (x ∷ xs) = f x >>= λ _ → mapM! f xs
  19. main : IO Unit
  20. main = mapM! printNat (sort (downFrom 1200))
  21. -- 1000 0.6s
  22. -- 2000 4.8s
  23. -- 4000 36.2s