[Haskell-beginners] splitWith and foldr stack overflow
Sean Bartell
wingedtachikoma at gmail.com
Sun Mar 22 12:26:35 EDT 2009
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?
More information about the Beginners
mailing list