-
Notifications
You must be signed in to change notification settings - Fork 0
/
Listy.hs
38 lines (31 loc) · 1.09 KB
/
Listy.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
myConcat :: [[a]] -> [a]
myConcat = foldr (++) []
myTakeWhile :: (a -> Bool) -> [a] -> [a]
myTakeWhile _ [] = []
myTakeWhile p (x:xs) = if p x
then x : (myTakeWhile p xs)
else []
foldTakeWhile :: (a -> Bool) -> [a] -> [a]
foldTakeWhile p = foldr step []
where step x xs = if p x
then x : xs
else []
myGroupBy :: (a -> a -> Bool) -> [a] -> [[a]]
myGroupBy comp xs = foldr step [] xs
where step x ((y:ys):xss)
| comp x y = (x:y:ys):xss
| otherwise = [x]:(y:ys):xss
step x xss = [x]:xss
myCycle :: [a] -> [a]
myCycle xs = foldr (++) (myCycle xs) [xs]
myAny :: (a -> Bool) -> [a] -> Bool
myAny p xs = foldr (\x b -> p x || b) False xs
myWords :: String -> [String]
myWords = foldr step []
where step c xss | isWhiteSpace c = []:xss
step c (xs:xss) = (c:xs):xss
step c [] = [[c]]
isWhiteSpace :: Char -> Bool
isWhiteSpace c = or [ c == ' ' , c == '\n', c == '\t']
unlines :: [String] -> String
unlines = foldr (\l p -> l ++ '\n':p) []