Although this sort works on Int lists < length million, when the stack overflows, the last clause of the merge function definition looks odd to me, recursing in 2 places. Is there clearer way to define that definition and how can the stack limitation be avoided?
msort :: Ord a => [a] -> [a]
msort [] = []
msort xs = concat . merge . split $ xs
merge :: Ord a => [[a]] -> [[a]]
merge [] = []
merge [x] = [x]
merge (l1:l2:ls) = merge $ mpair l1 l2 : merge ls
mpair :: Ord a => [a] -> [a] -> [a]
mpair [] l2 = l2
mpair l1 [] = l1
mpair l1@(x:xs) l2@(y:ys) | x >= y = x : mpair xs l2
| otherwise = y : mpair l1 ys
split :: [a] -> [[a]]
split xs = [[x]| x<-xs]