-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter6.hs
80 lines (65 loc) · 1.5 KB
/
Chapter6.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-- Exercise 1
fac :: Int -> Int
fac 0 = 1
fac n | n > 0 = n * fac (n - 1)
-- Exercise 2
sumdown :: Int -> Int
sumdown 0 = 0
sumdown n = n + sumdown (n-1)
-- Exercise 3
{-- (?) :: Int -> Int -> Int
1 ? _ = 1
x ? 1 = x
_ ? 0 = 1
x ? y = x * x ? (y - 1) --}
-- Exercise 4
euclid :: Int -> Int -> Int
euclid x y | x == y = x
| x > y = euclid (x - y) y
| x < y = euclid (y - x) x
| otherwise = 0
-- Exercise 5
ando :: [Bool] -> Bool
ando [] = True
ando (x:xs) = x && ando xs
{--concato :: [[a]] -> [a]
concato [[]] = []
concato (x:xs) = x : concato xs --}
{--replicateo :: Int -> a -> [a]
replicateo 0 x = []
replicateo n x = x : replicateo (n-1) x
(?) :: [a] -> Int -> a
(x:xs) ? 0 = x
(x:xs) ? n = xs ? (n-1)--}
elemo :: Eq a => a -> [a] -> Bool
elemo _ [] = False
elemo e (x:xs) = (x == e) || elemo e xs
-- Exercise 7
merge :: Ord a => [a] -> [a] -> [a]
merge a [] = a
merge [] b = b
merge (a:as) (b:bs) | a <= b = a: merge as (b:bs)
| a > b = b: merge (a:as) bs
-- Exercise 8
halve :: [a] -> ([a],[a])
halve [] = ([],[])
halve x = splitAt (div (length x) 2) x
msort :: Ord a => [a] -> [a]
msort [] = []
msort (x:[]) = [x]
msort (x:y:[]) = merge [x] [y]
msort x = (\(x,y) -> merge (msort x) (msort y)) (halve x)
-- Exercise 9
-- a)
sumo :: Integral a => [a] -> a
sumo [] = 0
sumo (x:xs) = x + sumo xs
-- b)
takeo :: Int -> [a] -> [a]
takeo 0 _ = []
takeo _ [] = []
takeo n (x:xs) = x: take (n-1) xs
-- c)
lasto :: [a] -> a
lasto (x:[]) = x
lasto (_:xs) = lasto xs