-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_hello_recursion.hs
55 lines (43 loc) · 1.17 KB
/
04_hello_recursion.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
mymin :: (Ord a) => a -> a -> a
mymin x y
| x < y = x
| otherwise = y
mymax :: (Ord a) => a -> a -> a
mymax x y
| x > y = x
| otherwise = y
myminimum :: (Ord a) => [a] -> a
myminimum [] = error "No minimum"
myminimum [x] = x
myminimum (x:xs) = mymin x $ minimum xs
mymaximum :: (Ord a) => [a] -> a
mymaximum [] = error "No maximum"
mymaximum [x] = x
mymaximum (x:xs) = mymax x $ mymaximum xs
myreplicate :: Int -> a -> [a]
myreplicate n _
| n <= 0 = []
myreplicate n a = a : myreplicate (n-1) a
mytake :: Int -> [a] -> [a]
mytake n _
| n <= 0 = []
mytake n (x:xs) = x : mytake (n-1) xs
myreverse :: [a] -> [a]
myreverse [] = []
myreverse (x:xs) = myreverse xs ++ [x]
myrepeat :: a -> [a]
myrepeat x = x : myrepeat x
myzip :: [a] -> [b] -> [(a,b)]
myzip [] _ = []
myzip _ [] = []
myzip (x:xs) (y:ys) = (x,y) : myzip xs ys
myelem :: (Eq a) => a -> [a] -> Bool
myelem _ [] = False
myelem x (y:ys)
| x == y = True
| otherwise = myelem x ys
quickSort :: (Ord a) => [a] -> [a]
quickSort [] = []
quickSort (x:xs) = quickSort smaller ++ [x] ++ quickSort largerOrEqual
where smaller = [y | y <- xs, y < x]
largerOrEqual = [y | y <- xs, y >= x]