Thanks Neil. How do I add in another ~10 computations, or map a list of a 100 computations to the same input?<br><br>Isn't there a way to do this without one computation having to be aware of the other? <br><br>This feels like a situation Parsec users would find themselves in all the time. When you have a bunch of parsers in a 'choice', does the start of the input stream linger until the last parser is executed?
<br><br>Thanks,<br>Greg<br><br><br><div><span class="gmail_quote">On 3/27/06, <b class="gmail_sendername">Neil Mitchell</b> <<a href="mailto:firstname.lastname@example.org">email@example.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br><br>> Here would be a better example then.<br>><br>> f lst = show (sum (filter (> 1) lst), sum (filter (> 2) lst))<br><br>I suspected that you actually wanted to do something "cleverer" with
<br>the list, for the sake of argument, I'm going to change >1 to p1 and<br>>2 to p2 - to show how this can be done in the general case. With the<br>specific information you know about >1 vs >2 you can do better, but
<br>this gets across the general point:<br><br>f lst = show (sumPairs (>1) (>2) lst)<br><br>sumPairs :: (Int -> Bool) -> (Int -> Bool) -> [Int] -> (Int, Int)<br>sumPairs p1 p2  = (0, 0)<br>sumPairs p1 p2 (x:xs) = (add p1 a, add p2 b)
<br> where<br> (a,b) = sumPairs xs<br> add pred value = if pred x then x+value else value<br><br>[Untested, something like this should work]<br><br>You can actually arrive at this solution entirely be reasoning on the
<br>program, i.e. not coming up with a fresh definition.<br><br>The above code essentially follows your imperative pseudo code - I<br>think its constant space, but I'm not too sure...<br><br>Thanks<br><br>Neil<br></blockquote>