[Haskell-cafe] multiple computations, same input
Greg Fitzgerald
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
end
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))
Thanks,
Greg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org//pipermail/haskell-cafe/attachments/20060327/e71ca5f8/attachment.htm
More information about the Haskell-Cafe
mailing list