[Haskell-cafe] multiple computations, same input
garious at gmail.com
Mon Mar 27 18:10:18 EST 2006
> hold a part of the data in memory while you show the first one,
Here would be a better example then.
f lst = show (sum (filter (> 1) lst), sum (filter (> 2) lst))
It ought to be *possible* to compute both operations without holding onto
any of the list elements.
In the imperative world, you'd say:
sum1 = 0
sum2 = 0
for num in lst
sum1 += num if num > 1
sum2 += num if num > 2
puts sum1, sum2
One could probably hack it together with foldM, the State monad, and maybe
some strictness, but I'd like to make full use of laziness and stick to the
basic list operations if it at all possible.
I'm not so concerned with solving this particular problem as I am in
learning the purely functional technique for performing orthogonal
computations on the same input without introducing a space leak.
Maybe something like this?
arr (sum . filter (>1)) &&& arr (sum . filter (>2))
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe