Skip to content

Instantly share code, notes, and snippets.

@KJTsanaktsidis
Created June 24, 2014 07:06
Show Gist options
  • Save KJTsanaktsidis/c914757cf26672ac4b01 to your computer and use it in GitHub Desktop.
Save KJTsanaktsidis/c914757cf26672ac4b01 to your computer and use it in GitHub Desktop.
--Implements Mergesort
mergeLists :: (Ord a) => [a] -> [a] -> [a] -> [a]
mergeLists acc xa xb
| null xa && null xb = acc --We are complete
| null xa = mergeLists (acc:(head xb)) [] (tail xb) --Only b left
| null xb = mergeLists (acc:(head xa)) (tail xa) [] --Only a left
| (head xa) <= (head xb) = mergeLists (acc:(head xa)) (tail xa) xb --a first
| otherwise = mergeLists (acc:(head xb)) xa (tail xb) --b first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment