@@ -22,6 +22,27 @@ safeHead' xs = Just $ head xs
22
22
andEmpty = assert (and [] ) True
23
23
orEmpty = assert (not $ or [] ) False
24
24
25
+ -- Usefull functions dealing with strings
26
+ cutByLine = lines " multi\n line\n text"
27
+ concatByLine = unlines [" multi" , " line" , " text" ]
28
+ cutBySpace = words " Can \r you\n hear\t me,\r\n Alice?"
29
+ concatBySpace = unwords [" Can" , " you" , " hear" , " me," , " Alice?" ]
30
+ -- Furthermore:
31
+ -- `take', `drop' and `takeWhile', `dropWhile'
32
+ -- `splitAt' "tuples up" the results of `take' and `drop'
33
+ -- `span' "tuples up" those of `takeWhile'
34
+ -- `break' "tuples up" those of `dropWhile'
35
+
36
+ -- ex02, both work
37
+ splitWith :: (a -> Bool ) -> [a ] -> [[a ]]
38
+ splitWith _ [] = []
39
+ -- splitWith p all@(x:xs) = case break p all of
40
+ -- ([] , _ ) -> splitWith p xs
41
+ -- (first, rest) -> first : splitWith p rest
42
+ -- `dropWhile null' to remove null list at the head brought by `break'
43
+ splitWith p (x: xs) = dropWhile null $ first : restSplit
44
+ where (first, rest) = break p (x: xs)
45
+ restSplit = splitWith p $ dropWhile p rest
25
46
26
47
27
48
-- Failed to get a reasonably working `zipWithN'
0 commit comments