-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_ops.hs
38 lines (31 loc) · 985 Bytes
/
list_ops.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
myOr :: [Bool] -> Bool
myOr [] = False
myOr (x:xs) = x || myOr xs
myAny :: (a -> Bool) -> [a] -> Bool
myAny _ [] = False
myAny f (x:xs) = f x || myAny f xs
myElem :: Eq a => a -> [a] -> Bool
myElem _ [] = False
myElem a list = any (\x -> x == a) list
myReverse :: [a] -> [a]
myReverse [] = []
myReverse list = lastElem : myReverse remaining
where lastElem = last list
remainingLength = length list - 1
remaining = take remainingLength list
squish :: [[a]] -> [a]
--squish list = foldl (\a b -> a ++ b) [] list
squish [] = []
squish (xs:xss) = xs ++ squish xss
squishMap :: (a -> [b]) -> [a] -> [b]
squishMap _ [] = []
squishMap f (x:xs) = f x ++ squishMap f xs
squishAgain :: [[a]] -> [a]
squishAgain = squishMap id
myMaximumBy :: (a -> a -> Ordering) -> [a] -> a
myMaximumBy _ [] = error "empty list"
myMaximumBy f (x:xs) = g f x xs where
g _ currMax [] = currMax
g f currMax (x:xs)
| f currMax x == GT = g f currMax xs
| otherwise = g f x xs