0

How to code the following pseudo-code elegantly in Haskell?

for (i from 0 to 100):
    for (j from 0 to 100):
        k=0
        while ( f(i,j,k) >0 ):
            return (i,j,k)
            k+=1

where f is an unimportant function of i,jand k.

So it should output something like this: [(0,0,0),(0,0,1)..], a list with each element a tuple formed by i,jandk. (It's fine if it were of the form [[0,0,0],[0,0,1]...] instead of tuples.)

0

2 Answers 2

3

Assuming with return you actually mean Python-like yield, otherwise the algorithm wouldn't make sense.

Using do notation, this is very straightforward:

do
    i <- [0..100]
    j <- [0..100]
    k <- takeWhile (\k -> f i j k > 0) [0..]
    return (i, j, k)
Sign up to request clarification or add additional context in comments.

Comments

2

Untested:

[ (i,j,k) |
  i <- [0 .. 100],
  j <- [0 .. 100],
  k <- takeWhile (\k -> f i j k > 0) [0 ..] ]

This is a list comprehension that loops over i/j in the obvious way, and uses takeWhile to limit k by the result of f.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.