-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhigherOrder.hs
76 lines (46 loc) · 1.79 KB
/
higherOrder.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
multThree :: Int -> Int -> Int -> Int
multThree x y z = x * y * z
multTwoWithNine = multThree 9
compareWithHundred :: Int -> Ordering
compareWithHundred x = compare 100 x
compareWithHundred' = compare 100
divideByTen :: (Floating a) => a -> a
divideByTen = (/10)
isUpperAlphaNum :: Char -> Bool
isUpperAlphaNum = (`elem` "A..Z")
subtractFourFrom = (4 `subtract`)
applytwice :: (a -> a) -> a -> a
applytwice f x = f (f x)
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 x y = f y x -- This means f is a function that takes an argument of type a first and b, but if you give it an argument of b first and then an argument of type a, flip fixes this order.
map' :: (a -> b) -> [a] -> [b]
map' f [] = []
map' f (x:xs) = f x : map' f xs
filter' :: (a -> Bool) -> [a] -> [a]
filter' p [] = []
filter' p (x:xs)
| p x = x : filter' p xs
| otherwise = filter' p xs
quicksort' :: (Ord a) => [a] -> [a]
quicksort' [] = []
quicksort' (x:xs) = let smaller = filter (<=x) xs
bigger = filter (>x) xs
in quicksort' smaller ++ [x] ++ quicksort' bigger
-- largest number under 100,000 that's divisible by 3829
largestDivisible :: Integer
largestDivisible = head (filter p [99999, 99998..])
where p x = x `mod` 3829 == 0
allSquares = map (^2) [1..]
oddSquares = filter odd allSquares
smallerThan = takeWhile (<10000) oddSquares
sumOfAll = sum smallerThan
-- For all starting numbers between 1 and 100, how many Collatz chains have a length greater than 15?
chain :: Integer -> [Integer]
chain 1 = [1]
chain n
| even n = n:chain (n `div` 2)
| odd n = n:chain (n+3 + 1)