Recursive Fibonacci with yield
up vote
0
down vote
favorite
i am trying to build a Fibonacci function with yield here in this code, my
problem is
How to use yield in recursion and recursive calls
def fib(x):
if(x==0 or x==1 ):
yield 1
else:
yield fib(x-1)+fib(x-2)
y=[i for i in fib(10)]
print(y);
I get this error
"unsupported operand type(s) for +: 'generator' and 'generator'"
I am in need to know how to use yield with recursion without get this error
python python-3.x recursion fibonacci yield
|
show 2 more comments
up vote
0
down vote
favorite
i am trying to build a Fibonacci function with yield here in this code, my
problem is
How to use yield in recursion and recursive calls
def fib(x):
if(x==0 or x==1 ):
yield 1
else:
yield fib(x-1)+fib(x-2)
y=[i for i in fib(10)]
print(y);
I get this error
"unsupported operand type(s) for +: 'generator' and 'generator'"
I am in need to know how to use yield with recursion without get this error
python python-3.x recursion fibonacci yield
2
Possible duplicate of Closed form Fibonacci Series
– quant
Nov 11 at 14:19
2
Explained here in detail stackoverflow.com/questions/53244630/…
– quant
Nov 11 at 14:20
My problem is not about Fibonacci it's about using Yield with recursive calls ,and recursion
– Nader Elsayed
Nov 11 at 14:33
The linked answer has multiple solutions including one with yield
– quant
Nov 11 at 14:39
i found the answer but the topic us not related yo yield it's about recursion.
– Nader Elsayed
Nov 11 at 14:47
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
i am trying to build a Fibonacci function with yield here in this code, my
problem is
How to use yield in recursion and recursive calls
def fib(x):
if(x==0 or x==1 ):
yield 1
else:
yield fib(x-1)+fib(x-2)
y=[i for i in fib(10)]
print(y);
I get this error
"unsupported operand type(s) for +: 'generator' and 'generator'"
I am in need to know how to use yield with recursion without get this error
python python-3.x recursion fibonacci yield
i am trying to build a Fibonacci function with yield here in this code, my
problem is
How to use yield in recursion and recursive calls
def fib(x):
if(x==0 or x==1 ):
yield 1
else:
yield fib(x-1)+fib(x-2)
y=[i for i in fib(10)]
print(y);
I get this error
"unsupported operand type(s) for +: 'generator' and 'generator'"
I am in need to know how to use yield with recursion without get this error
python python-3.x recursion fibonacci yield
python python-3.x recursion fibonacci yield
edited Nov 11 at 14:30
asked Nov 11 at 14:17
Nader Elsayed
6615
6615
2
Possible duplicate of Closed form Fibonacci Series
– quant
Nov 11 at 14:19
2
Explained here in detail stackoverflow.com/questions/53244630/…
– quant
Nov 11 at 14:20
My problem is not about Fibonacci it's about using Yield with recursive calls ,and recursion
– Nader Elsayed
Nov 11 at 14:33
The linked answer has multiple solutions including one with yield
– quant
Nov 11 at 14:39
i found the answer but the topic us not related yo yield it's about recursion.
– Nader Elsayed
Nov 11 at 14:47
|
show 2 more comments
2
Possible duplicate of Closed form Fibonacci Series
– quant
Nov 11 at 14:19
2
Explained here in detail stackoverflow.com/questions/53244630/…
– quant
Nov 11 at 14:20
My problem is not about Fibonacci it's about using Yield with recursive calls ,and recursion
– Nader Elsayed
Nov 11 at 14:33
The linked answer has multiple solutions including one with yield
– quant
Nov 11 at 14:39
i found the answer but the topic us not related yo yield it's about recursion.
– Nader Elsayed
Nov 11 at 14:47
2
2
Possible duplicate of Closed form Fibonacci Series
– quant
Nov 11 at 14:19
Possible duplicate of Closed form Fibonacci Series
– quant
Nov 11 at 14:19
2
2
Explained here in detail stackoverflow.com/questions/53244630/…
– quant
Nov 11 at 14:20
Explained here in detail stackoverflow.com/questions/53244630/…
– quant
Nov 11 at 14:20
My problem is not about Fibonacci it's about using Yield with recursive calls ,and recursion
– Nader Elsayed
Nov 11 at 14:33
My problem is not about Fibonacci it's about using Yield with recursive calls ,and recursion
– Nader Elsayed
Nov 11 at 14:33
The linked answer has multiple solutions including one with yield
– quant
Nov 11 at 14:39
The linked answer has multiple solutions including one with yield
– quant
Nov 11 at 14:39
i found the answer but the topic us not related yo yield it's about recursion.
– Nader Elsayed
Nov 11 at 14:47
i found the answer but the topic us not related yo yield it's about recursion.
– Nader Elsayed
Nov 11 at 14:47
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
You want the power to shoot yourself in the foot.
Well, here you go.
Introducing "yield from" in python 3.3+ in PEP 380
"forward recursive yield"
(This will behave similar to how you would expect generators to behave.)
def fib_infinity(start = 0, acc = 1):
yield start + acc
yield from fib_infinity(acc, start + acc)
i = fib_infinity()
next(i) #1
next(i) #2
next(i) #3
next(i) #5
next(i) #8
Note that this will error out once the maximum recursion depth is reached.
This however does not really satisfy how we tend to think of a usual recursive function that tries to work downwards. However, it seems that we could simplify our recursive function to a tail recursive function, we could introduce yield and utilize it.
Attempt 2:
"backward recursive yield"
def fib(n, a = 0, b = 1):
if n == 0:
yield a
if n == 1:
yield b
yield from fib(n - 1, b, a + b)
y = [next(fib(i)) for i in range(10)]
#[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
note however that we get our output in one "next" call. What happens now with a yield let loose?
i = fib(8)
next(i) #21
next(i) #21
next(i) #RecursionError: maximum recursion depth exceeded in comparison
We can make the function very slightly safer by introducing a return, for a final version.
Attempt 3: #safe for non-base cases.
def fib(n, a = 0, b = 1):
if n == 0:
yield a
return 0
if n == 1:
yield b
return 0
yield from fib(n - 1, b, a + b)
i = fib(8)
next(i) #21
next(i) #StopIteration
I cannot think of a single scenario where you would want to create a recursive solution with yields, and the downsides of the setup seem immense. However, somethings are just meant to be explored for fun. This question made me curious enough to do some research on it. I will advise however, to never actually do this.
thank you for your answer , i was to know how to recurs with yield i am new in use it , yes i was try that for learn more and fun and i find that your Attempt was easier to understand the idea , so thank you again
– Nader Elsayed
Nov 11 at 23:00
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You want the power to shoot yourself in the foot.
Well, here you go.
Introducing "yield from" in python 3.3+ in PEP 380
"forward recursive yield"
(This will behave similar to how you would expect generators to behave.)
def fib_infinity(start = 0, acc = 1):
yield start + acc
yield from fib_infinity(acc, start + acc)
i = fib_infinity()
next(i) #1
next(i) #2
next(i) #3
next(i) #5
next(i) #8
Note that this will error out once the maximum recursion depth is reached.
This however does not really satisfy how we tend to think of a usual recursive function that tries to work downwards. However, it seems that we could simplify our recursive function to a tail recursive function, we could introduce yield and utilize it.
Attempt 2:
"backward recursive yield"
def fib(n, a = 0, b = 1):
if n == 0:
yield a
if n == 1:
yield b
yield from fib(n - 1, b, a + b)
y = [next(fib(i)) for i in range(10)]
#[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
note however that we get our output in one "next" call. What happens now with a yield let loose?
i = fib(8)
next(i) #21
next(i) #21
next(i) #RecursionError: maximum recursion depth exceeded in comparison
We can make the function very slightly safer by introducing a return, for a final version.
Attempt 3: #safe for non-base cases.
def fib(n, a = 0, b = 1):
if n == 0:
yield a
return 0
if n == 1:
yield b
return 0
yield from fib(n - 1, b, a + b)
i = fib(8)
next(i) #21
next(i) #StopIteration
I cannot think of a single scenario where you would want to create a recursive solution with yields, and the downsides of the setup seem immense. However, somethings are just meant to be explored for fun. This question made me curious enough to do some research on it. I will advise however, to never actually do this.
thank you for your answer , i was to know how to recurs with yield i am new in use it , yes i was try that for learn more and fun and i find that your Attempt was easier to understand the idea , so thank you again
– Nader Elsayed
Nov 11 at 23:00
add a comment |
up vote
1
down vote
You want the power to shoot yourself in the foot.
Well, here you go.
Introducing "yield from" in python 3.3+ in PEP 380
"forward recursive yield"
(This will behave similar to how you would expect generators to behave.)
def fib_infinity(start = 0, acc = 1):
yield start + acc
yield from fib_infinity(acc, start + acc)
i = fib_infinity()
next(i) #1
next(i) #2
next(i) #3
next(i) #5
next(i) #8
Note that this will error out once the maximum recursion depth is reached.
This however does not really satisfy how we tend to think of a usual recursive function that tries to work downwards. However, it seems that we could simplify our recursive function to a tail recursive function, we could introduce yield and utilize it.
Attempt 2:
"backward recursive yield"
def fib(n, a = 0, b = 1):
if n == 0:
yield a
if n == 1:
yield b
yield from fib(n - 1, b, a + b)
y = [next(fib(i)) for i in range(10)]
#[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
note however that we get our output in one "next" call. What happens now with a yield let loose?
i = fib(8)
next(i) #21
next(i) #21
next(i) #RecursionError: maximum recursion depth exceeded in comparison
We can make the function very slightly safer by introducing a return, for a final version.
Attempt 3: #safe for non-base cases.
def fib(n, a = 0, b = 1):
if n == 0:
yield a
return 0
if n == 1:
yield b
return 0
yield from fib(n - 1, b, a + b)
i = fib(8)
next(i) #21
next(i) #StopIteration
I cannot think of a single scenario where you would want to create a recursive solution with yields, and the downsides of the setup seem immense. However, somethings are just meant to be explored for fun. This question made me curious enough to do some research on it. I will advise however, to never actually do this.
thank you for your answer , i was to know how to recurs with yield i am new in use it , yes i was try that for learn more and fun and i find that your Attempt was easier to understand the idea , so thank you again
– Nader Elsayed
Nov 11 at 23:00
add a comment |
up vote
1
down vote
up vote
1
down vote
You want the power to shoot yourself in the foot.
Well, here you go.
Introducing "yield from" in python 3.3+ in PEP 380
"forward recursive yield"
(This will behave similar to how you would expect generators to behave.)
def fib_infinity(start = 0, acc = 1):
yield start + acc
yield from fib_infinity(acc, start + acc)
i = fib_infinity()
next(i) #1
next(i) #2
next(i) #3
next(i) #5
next(i) #8
Note that this will error out once the maximum recursion depth is reached.
This however does not really satisfy how we tend to think of a usual recursive function that tries to work downwards. However, it seems that we could simplify our recursive function to a tail recursive function, we could introduce yield and utilize it.
Attempt 2:
"backward recursive yield"
def fib(n, a = 0, b = 1):
if n == 0:
yield a
if n == 1:
yield b
yield from fib(n - 1, b, a + b)
y = [next(fib(i)) for i in range(10)]
#[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
note however that we get our output in one "next" call. What happens now with a yield let loose?
i = fib(8)
next(i) #21
next(i) #21
next(i) #RecursionError: maximum recursion depth exceeded in comparison
We can make the function very slightly safer by introducing a return, for a final version.
Attempt 3: #safe for non-base cases.
def fib(n, a = 0, b = 1):
if n == 0:
yield a
return 0
if n == 1:
yield b
return 0
yield from fib(n - 1, b, a + b)
i = fib(8)
next(i) #21
next(i) #StopIteration
I cannot think of a single scenario where you would want to create a recursive solution with yields, and the downsides of the setup seem immense. However, somethings are just meant to be explored for fun. This question made me curious enough to do some research on it. I will advise however, to never actually do this.
You want the power to shoot yourself in the foot.
Well, here you go.
Introducing "yield from" in python 3.3+ in PEP 380
"forward recursive yield"
(This will behave similar to how you would expect generators to behave.)
def fib_infinity(start = 0, acc = 1):
yield start + acc
yield from fib_infinity(acc, start + acc)
i = fib_infinity()
next(i) #1
next(i) #2
next(i) #3
next(i) #5
next(i) #8
Note that this will error out once the maximum recursion depth is reached.
This however does not really satisfy how we tend to think of a usual recursive function that tries to work downwards. However, it seems that we could simplify our recursive function to a tail recursive function, we could introduce yield and utilize it.
Attempt 2:
"backward recursive yield"
def fib(n, a = 0, b = 1):
if n == 0:
yield a
if n == 1:
yield b
yield from fib(n - 1, b, a + b)
y = [next(fib(i)) for i in range(10)]
#[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
note however that we get our output in one "next" call. What happens now with a yield let loose?
i = fib(8)
next(i) #21
next(i) #21
next(i) #RecursionError: maximum recursion depth exceeded in comparison
We can make the function very slightly safer by introducing a return, for a final version.
Attempt 3: #safe for non-base cases.
def fib(n, a = 0, b = 1):
if n == 0:
yield a
return 0
if n == 1:
yield b
return 0
yield from fib(n - 1, b, a + b)
i = fib(8)
next(i) #21
next(i) #StopIteration
I cannot think of a single scenario where you would want to create a recursive solution with yields, and the downsides of the setup seem immense. However, somethings are just meant to be explored for fun. This question made me curious enough to do some research on it. I will advise however, to never actually do this.
answered Nov 11 at 16:19
Paritosh Singh
4908
4908
thank you for your answer , i was to know how to recurs with yield i am new in use it , yes i was try that for learn more and fun and i find that your Attempt was easier to understand the idea , so thank you again
– Nader Elsayed
Nov 11 at 23:00
add a comment |
thank you for your answer , i was to know how to recurs with yield i am new in use it , yes i was try that for learn more and fun and i find that your Attempt was easier to understand the idea , so thank you again
– Nader Elsayed
Nov 11 at 23:00
thank you for your answer , i was to know how to recurs with yield i am new in use it , yes i was try that for learn more and fun and i find that your Attempt was easier to understand the idea , so thank you again
– Nader Elsayed
Nov 11 at 23:00
thank you for your answer , i was to know how to recurs with yield i am new in use it , yes i was try that for learn more and fun and i find that your Attempt was easier to understand the idea , so thank you again
– Nader Elsayed
Nov 11 at 23:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249612%2frecursive-fibonacci-with-yield%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Possible duplicate of Closed form Fibonacci Series
– quant
Nov 11 at 14:19
2
Explained here in detail stackoverflow.com/questions/53244630/…
– quant
Nov 11 at 14:20
My problem is not about Fibonacci it's about using Yield with recursive calls ,and recursion
– Nader Elsayed
Nov 11 at 14:33
The linked answer has multiple solutions including one with yield
– quant
Nov 11 at 14:39
i found the answer but the topic us not related yo yield it's about recursion.
– Nader Elsayed
Nov 11 at 14:47