Learn You a Haskell demonstrates the break
function:
breaks it when the predicate is first true.
Example:
-- ghci> break (==4) [1,2,3,4,5,6,7]
-- ([1,2,3],[4,5,6,7])
Here's my code:
break' :: (a -> Bool) -> [a] -> ([a], [a])
break' _ [] = ([], [])
break' f ys = break'' f ys []
where break'' _ [] acc = (reverse acc, [])
break'' g xxs@(x:xs) acc
| g x = (reverse acc, xxs)
| otherwise = break'' g xs (x:acc)
I used the reverse
rather than use the ++
function due to @Anonymous's helpful advice on my implementation of lines.
Please review it.