Skip to content

Instantly share code, notes, and snippets.

@ctford
Created July 2, 2014 18:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctford/487f1c63ac63e3e22523 to your computer and use it in GitHub Desktop.
Save ctford/487f1c63ac63e3e22523 to your computer and use it in GitHub Desktop.
A length-safe insertion sort.
insert : Ord a => a -> Vect n a -> Vect (1 + n) a
insert x [] = [x]
insert x (y::ys) with (x <= y)
| True = x::y::ys
| False = y::insert x ys
isort : Ord a => Vect n a -> Vect n a
isort [] = []
isort (x::xs) = insert x (isort xs)
@ctford
Copy link
Author

ctford commented Jul 2, 2014

This isn't tail-recursive, so careful on memory usage!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment