Skip to content

Commit dbe48af

Browse files
committed
RWH ch04, ex02 (I'm so dumb)
1 parent e2c9033 commit dbe48af

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

rwh04.hs

+21
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@ safeHead' xs = Just $ head xs
2222
andEmpty = assert (and []) True
2323
orEmpty = assert (not $ or []) False
2424

25+
-- Usefull functions dealing with strings
26+
cutByLine = lines "multi\nline\ntext"
27+
concatByLine = unlines ["multi", "line", "text"]
28+
cutBySpace = words "Can \ryou\nhear\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
2546

2647

2748
-- Failed to get a reasonably working `zipWithN'

0 commit comments

Comments
 (0)