
22 Mar
2009
22 Mar
'09
12:26 p.m.
I wrote a splitWith function (from RWH) and then converted it to use foldr: splitWith, splitWith2 :: (a -> Bool) -> [a] -> [[a]] splitWith p xs = let (ls, ts) = f xs in ts:ls where f [] = ([], []) f (x:xs) | p x = (ls, x:ts) | otherwise = (ts:ls, []) where (ls, ts) = f xs splitWith2 p xs = let (ls, ts) = foldr f ([],[]) xs in ts:ls where f x (ls, ts) | p x = (ls, x:ts) | otherwise = (ts:ls, []) Tested with something simple like take 8 $ splitWith id (cycle [False,True]) splitWith works fine, but splitWith2 causes a stack overflow. Don't they have the same recursive structure, though?