What's the difference between recursion and corecursion?

What is the difference between Iteration and recursion?

  • Answer:

    The original answer really doesn't explain much, and almost nothing that's relevant to the question. (Sorry!) The part about iterations typically involving some kind of loop is on target, though it doesn't really shed much light. This might best be explained by giving two pseudo-code examples, a function to calculate the factorial of an input argument. For simplicity's sake, since this is just to show examples, we will assume that we can be sure that the argument is a positive integer. # This is an iterative function to calculate the factorial Function I-Factorial(x) Result = 1 For i = 1 to x Result = Result * i Next i Return Result End Function # This is a recursive function to calculate the factorial Function R-Factorial(x) If x = 1 then Return 1 Else Return x * R-Factorial(x-1) End If End Function Instead of stepping through a loop, the recursive function simply invokes itself, each invocation using an argument that's been reduced by 1 until ultimately it receives an argument of "1". Then the call for "R-Factorial(1)" is returned to the call for "R-Factorial(2)", which passes its result up to the call for "R-Factorial(3)", which passes its result up to the call for "R-Factorial(4)", and so on, until the accumulated result has bubbled its way up to the original call for "R-Factorial(x)". As noted in the original answer, recursive functions can (but do not have to) incur additional overhead in terms of call stacks, memory allocations, and so on. If you write in assembly language (as I used to) and utilize reentrant coding techniques, the overhead is very close to zero. ========= 1. Recursion: # When a recursive call is made, the method/process copies or clones itself, making new copy of: * the code * the local variables (with their initial values), * the parameters 2. Iteration : there is no recursive call involved that saves a lot of time and space too as no extra space is needed to store each copy generated in recursion. Iterative codes usually refers to codes that contain explicit iteration processes, that is, loops.

wiki.answers.com Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.