Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

It is something of an axiom that any recursive solution can be rewritten as an iterative one. Often a finite infinite loop is useful:

#!/usr/bin/perl -w use constant HIGH => 10_000; my $ans = int(rand(HIGH)) + 1; my ( $lower, $higher ) = (1, HIGH); while(1) { my $guess = int(($lower + $higher)/2); print "Guessing: $guess\n"; last if $ans == $guess; if ( $guess > $ans ) { print "Lower..."; $higher = $guess -1; } else { print "Higher..."; $lower = $guess +1; } } print "The guess was correct!";

FWIW, with your code &function(args) syntax is not considered best style, you can drop the & - which you actually do in the sub. A consistent style is a good idea. You could declare and set $ans in one call.

It is worth noting that while an iterative solution will almost inevitably run faster than a recursive solution, a good recursive solution is often quite terse. A classic very simple example is the calculation of factorial n! The factorial is defined n! = 1 x 2 x 3 x .... (n-1) x n You can code using either iteration or recursion.....

sub fact_rec{ my ($num) = @_; $num ? $num*fact_rec($num-1) : 1 } sub fact_it{ my ($num) = @_; my $fac = 1; for my $i( 1 .. $num ) { $fac *= $i; } return $fac; } for (1..10){ printf"%d!\t%10d\t%10d\n", $_, fact_rec($_), fact_it($_); }

As you can see the recursive solution is short and sweet. It also has a bug that can cause it to go infinite. One gotcha with recursion is that you need to be *positive* that your exit condition (ie stop recursing when we are done) will be *always* be met. A typical real world use for recursion is in dealing with tree like structures
The bug:Negative numbers or floats mean that $num -1 will never equal 0, and so always it will always be true.

cheers

tachyon


In reply to Re: Behold! The power of recursion. by tachyon
in thread Behold! The power of recursion. by DigitalKitty

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (12)
    As of 2014-07-23 06:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (133 votes), past polls