Recursion vs Iteration

Discussion in 'Archives' started by Faskist, Apr 10, 2008.

Recursion vs Iteration
  1. Unread #1 - Apr 10, 2008 at 9:12 PM
  2. Faskist
    Joined:
    Apr 25, 2005
    Posts:
    1,869
    Referrals:
    0
    Sythe Gold:
    0

    Faskist Tuxhead
    Banned

    Recursion vs Iteration

    You may have noticed a guide by TDD on iteration in the Delphi programming language. It shows how to perform a basic looping function in a very efficient manner.

    Iteration, however, is not the only loop structure allowed in Delphi, and most other programming languages. Lending a functional hand, so to speak, is the idea of recursion. Recursion is a programming concept whereby a function of program calls, or executes, itself. This may seem like an odd concept, but it is easiest to illustrate in brute-force style math programs, such as a factorial program. The following is in Scheme;
    Code:
    (define (fact n)
        (if (= n 0)
            1
            (* n (fact (- n 1))))
    This might seem pretty impenetrable (It might scare the more C/C++/Java-oriented among you, what with all the parenthesi :p).

    What it does is define a procedure called "fact" (Short for factorial, in our case), which takes the argument "n". "n" is checked to see whether it is equal to 0, and, if it is, it returns 1. If not, it recurses, and here we see where recursion can become confusing, as the expression
    Code:
    (* n (fact (- n 1)))
    reduces down to
    Code:
    (* n [the result of evaluating fact again, if it is true])
    This can quickly become hugely complex to envision, but it can help to imagine a long string of (fact (- n 1)), until we reach the correct answer, when we begin the process of multiplying "n" by each successive answer.

    Each time a recursive process calls itself, it stores a copy of itself in memory. In essence, it expands the procedural call. An example;
    Code:
    (* n (fact (- (* n (fact (- (* n (fact (- n 1))) 1))) 1)))
    The above is the example code for the final call, (* n (fact (- n 1))), if we assume "n" = 3.

    As you can see, recursion is memory-intensive. It stores the entire loop within itself -- you can expose a flaw of C by creating an infinite recursive loop and watching it crash as it runs out of stack space. A stack is an abstract data type, which stores our instructions to the computer. When it overflows, the program dies and our operating system frees the resources.

    Recursion's problem of stack space can be dealt with by using tail recursion, a method of writing your recursion in such a way that the program executes in constant space, ie. it always takes the same amount of memory. This is considered an iterative loop, because the memory required does not increase in line with the number of recursive calls.

    Recursive loops, for mathematical functions, are very succinct. An iterative loop would require more explicit tests to be made, in a less orderly fashion, but would be more efficient.

    Iteration is much simpler. While recursion pushes the operation onto the stack until it can be popped for execution, iteration simply moves the pointer to where the operation is stored. Consequently, it can be much, much faster, but iterative loops are typically ugly, and do not track their current state. Ackermann functions, tree traversal and Quicksort are all examples of problems which cannot effectively use an iterative solution. Any iterative solution would need to use a stack, lumbering it with the same problems as a recursive solution.

    Iterative loops typically look like this (Examples given in C);
    Code:
    while(condition) {
      action(argument);
    }
    For loops are a little different.
    Code:
    for(variable-initialisation;variable-condition;variable-increment/decrement) {
        action(arguments);
    }
    I hope you've learnt a little from this, and not gotten too lost. Post any problems, inaccuracies and missing information you see from this guide on the thread, and I'll edit it in. Thanks.
     
  3. Unread #2 - Apr 10, 2008 at 9:13 PM
  4. Eric
    Joined:
    Feb 23, 2007
    Posts:
    3,344
    Referrals:
    13
    Sythe Gold:
    25

    Eric Grand Master
    Banned

    Recursion vs Iteration

    Great guide, Fascist!
     
  5. Unread #3 - Apr 13, 2008 at 9:31 PM
  6. Fouisgras
    Joined:
    Jan 21, 2007
    Posts:
    2,202
    Referrals:
    2
    Sythe Gold:
    0

    Fouisgras Jansen's Lover
    $5 USD Donor

    Recursion vs Iteration

    This isn't delphi you dumb bitches. Moved.
     
  7. Unread #4 - May 17, 2008 at 1:19 AM
  8. ssj_ion
    Referrals:
    0

    ssj_ion Guest

    Recursion vs Iteration

    i think its awsome
     
  9. Unread #5 - Aug 30, 2008 at 3:23 PM
  10. joking
    Joined:
    Nov 9, 2007
    Posts:
    736
    Referrals:
    3
    Sythe Gold:
    0

    joking Apprentice
    Do Not Trade

    Recursion vs Iteration

    looks good and straight forward. i enjoyed reading this thx
     
  11. Unread #6 - Nov 3, 2008 at 4:20 PM
  12. Noob!
    Joined:
    Aug 29, 2008
    Posts:
    322
    Referrals:
    0
    Sythe Gold:
    0

    Noob! Forum Addict
    Banned

    Recursion vs Iteration

    great guide 8/10
     
< Screamer's Nameshop - Fast and safe | Basics of Perl! >

Users viewing this thread
1 guest


 
 
Adblock breaks this site