Skip to content

Instantly share code, notes, and snippets.

@chomado
Created November 2, 2014 04:57
Show Gist options
  • Select an option

  • Save chomado/6d9704e4a64ce33c9183 to your computer and use it in GitHub Desktop.

Select an option

Save chomado/6d9704e4a64ce33c9183 to your computer and use it in GitHub Desktop.
insert :: Ord a => a -> [a] -> [a]
insert x [] = [x]
insert x (y : ys)
| x <= y = x:y:ys
| otherwise = y: insert x ys
isort :: Ord a => [a] -> [a]
isort [] = []
isort (x:xs) = insert x $ isort xs
main = do
print $ insert 3 [1 .. 10]
-- [1,2,3,3,4,5,6,7,8,9,10]
print $ isort [4, 6, 9, 8, 3, 5, 1, 7, 2]
-- [1,2,3,4,5,6,7,8,9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment