summaryrefslogtreecommitdiff
path: root/progs/demo/merge.hs
blob: cf61f8f41a55a7d47302e9c4b71414176bdeb4a8 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{- This is a simple merge sort -}

module Merge where
                
merge :: [Int] -> [Int] -> [Int]
merge [] x = x  
merge x [] = x
merge l1@(a:b) l2@(c:d) | a < c     = a:(merge b l2)
			| otherwise = c:(merge l1 d)

half [] = []
half [x] = [x]
half (x:y:z) = x:r where r = half z

sort [] = []
sort [x] = [x]
sort l = merge (sort odds) (sort evens) where
	     odds = half l
	     evens = half (tail l)

main =
  appendChan stdout "Enter a list of integers separated by \",\"\n" abort $
  readChan stdin abort $ \ input ->
  appendChan stdout 
      (show (sort (read ("[" ++ (head (lines input)) ++ "]"))))
      abort done