

test = do

src print $ ordered $ sort $ sort $ reverse $ sort src





mergesort :: (a -> a -> Ordering) -> [a] -> [a]

mergesort cmp = mergesort' cmp . map (:[])



mergesort' :: (a -> a -> Ordering) -> [[a]] -> [a]

mergesort' cmp [] = []

mergesort' cmp [xs] = xs

mergesort' cmp xss = mergesort' cmp (merge_pairs cmp xss)



merge_pairs :: (a -> a -> Ordering) -> [[a]] -> [[a]]

merge_pairs cmp [] = []

merge_pairs cmp [xs] = [xs]

merge_pairs cmp (xs:ys:xss) = merge cmp xs ys : merge_pairs cmp xss



merge :: (a -> a -> Ordering) -> [a] -> [a] -> [a]

merge cmp [] ys = ys

merge cmp xs [] = xs

merge cmp (x:xs) (y:ys)

= case x `cmp` y of

GT -> y : merge cmp (x:xs) ys

_ -> x : merge cmp xs (y:ys)



"sort"



"s" "o" "r" "t"

"os" "rt"

"orst"





risers :: Ord a => [a] -> [[a]]

risers [] = []

risers [x] = [[x]]

risers (x:y:etc) = if x <= y then (x:s):ss else [x]:(s:ss)

where (s:ss) = risers (y:etc)



risers "sort"

["s","ort"]



"s" "ort"

"orst"



"abcd"

n

sort [1..n]

Int32

risers

Int

Ord

risers