-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.hs
55 lines (45 loc) · 1.39 KB
/
Main.hs
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-- option 1 (https://github.com/PiotrJustyna/haskell-anywhere):
-- ./ghci.bat C:\Users\piotr_justyna\Documents\github\programming-in-haskell\part1_chapter6_exercise8
-- option 2 (stack):
-- stack ghci
-- option 3 (ghci):
-- ghci
--
-- :load Main
main = do
putStrLn "halve ([] :: [Int])"
putStrLn . show $ halve ([] :: [Int])
putStrLn "halve [1]"
putStrLn . show $ halve [1]
putStrLn "halve [1, 2]"
putStrLn . show $ halve [1, 2]
putStrLn "halve [1, 2, 3]"
putStrLn . show $ halve [1, 2, 3]
putStrLn "msort ([] :: [Int])"
putStrLn . show $ msort ([] :: [Int])
putStrLn "msort [1]"
putStrLn . show $ msort [1]
putStrLn "msort [2, 1]"
putStrLn . show $ msort [2, 1]
putStrLn "msort [2, 1, 3, 0, 17, 5, 5, 4, 3, 45]"
putStrLn . show $ msort [2, 1, 3, 0, 17, 5, 5, 4, 3, 45]
msort :: Ord a => [a] -> [a]
msort [] = []
msort (x:[]) = [x]
msort (x1:x2:[])
| x1 <= x2 = [x1, x2]
| otherwise = [x2, x1]
msort x = merge (msort (fst half)) (msort (snd half))
where
half = halve x
halve :: [a] -> ([a], [a])
halve x = (take newLength x, drop newLength x)
where
newLength = ((length x) `div` 2)
merge :: Ord a => [a] -> [a] -> [a]
merge [] [] = []
merge [] y = y
merge x [] = x
merge (x:xs) (y:ys)
| x <= y = x : (merge xs (y:ys))
| otherwise = y : (merge (x:xs) ys)