-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathch6_002.hs
41 lines (33 loc) · 934 Bytes
/
ch6_002.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
and' :: [Bool] -> Bool
and' [] = True
and' (x:xs) = x && and' xs
concat' [] = []
concat' (x:xs) = x ++ concat' xs
replicate' :: Int -> a -> [a]
replicate' 0 _ = []
replicate' n x = [x] ++ replicate' (n-1) x
replicate'' :: Int -> a -> [a]
replicate'' 0 _ = []
replicate'' n x = x : replicate'' (n-1) x
ind :: [a] -> Int -> a
ind (x:_) 0 = x
ind (_:xs) n = ind xs (n-1)
elem' :: Eq a => a -> [a] -> Bool
elem' _ [] = False
elem' m (x:xs)
| m == x = True
| otherwise = elem' m xs
merge :: Ord a => [a] -> [a] -> [a]
merge [] ys = ys
merge xs [] = xs
merge (x:xs) (y:ys)
| x <= y = x : merge xs (y:ys)
| otherwise = y: merge (x:xs) ys
merge_sort :: Ord a => [a] -> [a]
merge_sort [] = []
merge_sort [x] = [x]
merge_sort xs = merge left right
where
half = (length xs `div` 2)
left = merge_sort (take half xs)
right = merge_sort (drop half xs)