I might have been not very clear in my last mail. I decided to post again, and
go straight to the point, with some small examples.
Consider the following function streams.
streams :: (Int->Bool, Int->Bool)->(Int, Int)->([Int],[Int])
streams (p,q) (x,y) = (xs',ys')
where
(xs,ys) = streams (p,q) ((x+y),(y-x))
xs' = if p x then x:xs else xs
ys' = if q y then y:xs else ys
- produces a pair of ('infinite') lists
- produced lists are not indepentent (you need to calculate elements of one
list to calculate elements of the other)
- in each recursive call an element is added to the 1st/2nd list iff it
satisfies a given (Int->Bool) function p/q
How should one consume (part of) both lists, avoiding space leaks?
A particular example of consuming both lists might be writing them to files:
main :: IO()
main = do
let (s1,s2)=stream ... -- stream applied to some arguments (p,q) (x,y)
p' = ...
q' = ...
writeFile "f1.txt" (show$ takeWhile p' s1)
writeFile "f2.txt" (show$ takeWhile q' s2)
In this example all elements of s2 required to evaluate (takeWhile p' s1) are
kept in memory, until the first file is writen. Notice that writing one
element from s1 and one from s2 successively might still cause space leaks to
arise. Fusing the consuming functions with the producer is a possible, but
IMO dirty, way out.
If my question doesn't seem to make sense for any reason, please tell me,
maybe I am missing something obvious here.
Thanks,
J.A.
_______________________________________________
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe