How to code the following pseudo-code in Haskell?
x=0
for (i from 0 to 100):
j=0
while ( f(i,j) >0 ):
x+= f(i,j)
j+=1
(<
Here's a way that only computes f
once for each pair:
inner i = sum $ takeWhile (> 0) $ map (f i) [0..]
x= sum $ map inner [0..100]
I dislike list comprehensions, especially for more complex expressions, so I found your solution difficult to read. The primary difference is that instead of storing a list of j
s such that f i j > 0
, I stored the actual function value. This does no more work due to laziness.