Skip to content

Instantly share code, notes, and snippets.

@thegedge
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thegedge/ae6cadc8ea79934ed339 to your computer and use it in GitHub Desktop.
Save thegedge/ae6cadc8ea79934ed339 to your computer and use it in GitHub Desktop.
My amateur attempt at writing merge sort in Haskell.
merge :: Ord a => [a] -> [a] -> [a]
merge [] l = l
merge (a:l1) (b:l2)
| a <= b = a : merge l1 (b:l2)
| otherwise = b : merge l2 (a:l1)
mergeSort :: Ord a => [a] -> [a]
mergeSort [] = []
mergeSort [a] = [a]
mergeSort l =
merge (mergeSort $ take half l) (mergeSort $ drop half l)
where half = quot (length l) 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment