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










share|improve this question




















  • 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















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










share|improve this question




















  • 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













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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.






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer





















  • 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















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.






share|improve this answer





















  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation