2

I know there is generator yield in python like:

def f(n): 
    x = n
    while True:
        yield x
        x = x+1

So I try to convert this haskell function into python without using iterate: Haskell infinite recursion in list comprehension

I'm not sure how to define base case in python, also not sure how to combine if statement with this yield staff! here is what I try to do:

def orbit(x,y):
    while True:
        yield p (u,v)
        p (u,v) = (u^2 - v^2 + x, 2 * u * v + y)
Community
  • 1
  • 1
o1xhack
  • 87
  • 9

1 Answers1

3

I dont see where you're getting the p from. As far as I can see you can almost literally translate it from Haskell:

def orbit(x, y):
    u, v = 0, 0
    while True:
        u, v = u**2 − v**2 + x, 2*u*v + y
        yield u, v

In their example, calling the function as orbit(1, 2), u will be bound to 1 and v to 2 in the first round, then that ((1, 2)) is yielded. In the next iteration, u = 1**2 - 2**2 + 1 = 1 - 4 + 1 = -2 and v = 2*1*2 + 2 = 6.

L3viathan
  • 26,748
  • 2
  • 58
  • 81
  • this is so similar! almost exactly same, I think I suppose this in a difficult way. btw, thanks a lot! – o1xhack Oct 03 '16 at 21:59