Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Corecursion
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Fibonacci sequence === In the same way, the [[Fibonacci sequence]] can be represented as: :<math>a, b = (0, 1) : (b, a+b)</math> Because the Fibonacci sequence is a [[recurrence relation]] of order 2, the corecursive relation must track two successive terms, with the <math>(b, -)</math> corresponding to shift forward by one step, and the <math>(-, a+b)</math> corresponding to computing the next term. This can then be implemented as follows (using [[parallel assignment]]): <syntaxhighlight lang="python"> def fibonacci_sequence(): a, b = 0, 1 while True: yield a a, b = b, a + b </syntaxhighlight> In Haskell, <syntaxhighlight lang=haskell> map fst ( (\(a,b) -> (b,a+b)) `iterate` (0,1) )</syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)