
26 Jun
2005
26 Jun
'05
1:12 p.m.
Scott Turner wrote:
It's still possible to use fold and get short circuiting with good memory usage. upTo pred = foldr (\a -> \xs -> if pred a then [a] else a:xs) [] prodList = foldl' (*) 1 . upTo (== 0) It might be considered cheating, but AFAICT the test for ==0 needs to be separated from the multiplication proper.
They don't need to be separated, though the result is no longer as clear as one might wish: prodList xs = foldr (\x f a -> if x == 0 then 0 else f $! x * a) id xs 1 Udo. -- Wise men talk because they have something to say; fools, because they have to say something. -- Plato (429-347 BC)