-
Notifications
You must be signed in to change notification settings - Fork 0
/
baby.hs
110 lines (83 loc) · 2.85 KB
/
baby.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
-- chapter 1 ----------------------------------------------------------------
doubleMe x = x + x
doubleUs x y = doubleMe x + doubleMe y
doubleSmallNumber x = if x > 100 then x else x * 2
doubleSmallNumber' x = (if x > 100 then x else x * 2) + 1
rightTriangles = [(a, b, c) | c <- [1..10], a <- [1..c], b <- [1..a], a^2 + b^2 == c^2, a+b+c == 24]
-- chapter 4 ----------------------------------------------------------------
maximum' :: (Ord a) => [a] -> a
maximum' [] = error "error"
maximum' [x] = x
maximum' (x:xs) = max x (maximum' xs)
replicate' :: Int -> a -> [a]
replicate' n x
| n <= 0 = []
| otherwise = x : replicate' (n-1) x
take' :: Int -> [a] -> [a]
take' n _
| n <= 0 = []
take' _ [] = []
take' n (x:xs) = x : take' (n-1) xs
reverse' :: [a] -> [a]
reverse' [] = []
reverse' (x:xs) = (reverse' xs) ++ [x]
repeat' :: a -> [a]
repeat' x = x : repeat' x
zip' :: [a] -> [b] -> [(a, b)]
zip' [] _ = []
zip' _ [] = []
zip' (x:xs) (y:ys) = (x, y) : (zip' xs ys)
elm' :: (Eq a) => a -> [a] -> Bool
elm' a [] = False
elm' a (x:xs)
| a == x = True
| otherwise = elm' a xs
quickSort :: (Ord a) => [a] -> [a]
quickSort [] = []
quickSort (x:xs) =
let gte = [a | a <- xs, a >= x]
lt = [a | a <- xs, a < x]
in (quickSort lt) ++ [x] ++ (quickSort gte)
-- chapter 5 ----------------------------------------------------------------
zipWith' :: (a -> b-> c) -> [a] -> [b] -> [c]
zipWith' _ [] _ = []
zipWith' _ _ [] = []
zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys
flip' :: (a -> b -> c) -> b -> a -> c
flip' f y x = f x y
largestDivisible :: Integer
largestDivisible = head (filter (\x -> x `mod` 3829 == 0) [100000, 99999..])
chain :: Integer -> [Integer]
chain 1 = [1]
chain n
| even n = n : chain (n `div` 2)
| otherwise = n : chain (3 * n + 1)
numLongChains :: Int
numLongChains = length (filter (\xs -> length xs > 15) (map chain [1..100]))
-- 5.4 lambda
flipWithLambda :: (a -> b -> c) -> b -> a -> c
flipWithLambda f = \x y -> f y x
-- 5.5 fold
sum' :: (Num a) => [a] -> a
sum' xs = foldl (+) 0 xs
map' :: (a -> b) -> [a] -> [b]
map' f xs = foldr (\x acc -> f x : acc) [] xs
elm'' :: (Eq a) => a -> [a] -> Bool
elm'' y ys = foldr (\x acc -> if x == y then True else acc) False ys
maximum'' :: (Ord a) => [a] -> a
maximum'' = foldl1 max
reverse'' :: [a] -> [a]
-- reverse'' = foldl (\acc x -> x : acc) []
reverse'' = foldl (flip (:)) []
filter' :: (a -> Bool) -> [a] -> [a]
filter' p = foldr (\x acc -> if p x then x : acc else acc) []
last' :: [a] -> a
last' = foldl1 (\_ x -> x)
sqrtSums :: Int
-- sqrtSums = length (takeWhile (< 1000) (scanl1 (+) (map sqrt [1..]))) + 1
sqrtSums = (length . takeWhile (< 1000) $ scanl1 (+) $ map sqrt [1..]) + 1
-- refactor with $ .
before :: [Integer]
before = replicate 2 (product (map (*3) (zipWith max [1,2] [4,5])))
after :: [Integer]
after = replicate 2 . product . map (*3) $ zipWith max [1,2] [4,5]