streaming, state, etc...

Jorge Adriano jadrian@mat.uc.pt
Sun, 3 Nov 2002 19:04:41 +0000


Hi,=20
OK so I had completed my (search) algorithm implementation. And the 'last=
'=20
function was something like:  "until p f x0".=20
But now I wanted all iterations, I wanted to analyze the behavior of the=20
algorithm. What could I do? I thought about using  Trace... but it didn't=
=20
seem adequate, I wanted to manipulate and use that info... maybe monads..=
=2E=20
that would cluter my algorithm so bad...
This feeling lasted for a few seconds, then I thought, Lazy Evaluation!
"iterate p f x0" There, done :)
I had an infinite list, and I'd just take as much as I wanted.
*Analysis* and the *algorithm* itself were *completely separated*, yet it=
 was=20
using the information as soon as it was calculated - or maybe "it was bei=
ng=20
calculated only when it was needed". I could not do this kind of stuff in=
=20
imperative languages. Very nice :)

------------
Problem 1:
------------
I've used that many times since then, but lately it doesn't really seem t=
o=20
serve my needs. What if I need to get two different kinds of data, say=20
(list1, list2), and now I want to write them:

writeFile file1 (analyze list1)
writeFile file2 (analyze list2)

What if you need to evaluate list2 to evaluate list1? Not good!
I'm evaluating list2 and not consuming it, therefore space leak.
I could alternate between one list and another, but what if at a certain=20
point, evaluating the next elem in "list1" caused the evaluation of a few=
=20
thousand elements of "list2"?

So how do you usually deal with this kind of stuff? Concurrency??


------------
Problem 2:
------------
Yes  "iterate p f x0" looks good. What if function "f" is not a pure one?
If my monad is a Lazy one, streaming can be used(*), if it is not, that w=
on't=20
work. So streaming could work with monads like State, (lazy) ST. Not with=
 IO,=20
or (lazy) ST.

(*)=20
do
  list<-myalgorithm
  writeFile file1 (analyze list)


So, how do you usually do this kind of thing - extracting lists of info f=
rom=20
an algorithm?
J.A.