What I'm trying to achieve
As an exercise, I'm trying to write a 'mergeSorted' function which takes two lists, and returns a single list, in which all the elements from the two lists are sorted.
For example:
mergeSorted [2,6,5] [3,4,1]should return[1,2,3,4,5,6]mergeSorted [] [4,1]should return[4,1]
Here's what I wrote:
qsort :: Ord a => [a] -> [a]
qsort [] = []
qsort (x:xs) = qsort smaller ++ [x] ++ qsort larger
where
smaller = [a | a <- xs, a <= x]
larger = [b | b <- xs, b > x]
mergeSorted :: Ord a => [a] -> [a] -> [a]
mergeSorted listX [] = qsort listX
mergeSorted [] listY = qsort listY
mergeSorted listX listY | x <= y = x : mergeSorted xs (y:ys)
| otherwise = y : mergeSorted (x:xs) ys
where
(y:ys) = sortedYs
(x:xs) = sortedXs
sortedYs = qsort ys
sortedXs = qsort xs
The issue
The qsort code seems to be working well. But my mergeSorted isn't working.
If I execute mergeSorted with two lists which are not empty in GHCi, execution hangs forever. (i.e. I never get a result).
My question
Please can you tell me what's wrong with my mergeSorted code?