Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I naively expect above/below above/below above/below return sequence, rather than above/above/above below/below/below return.

print "above\n"; my $E = binary($k); print "below\n";

No, it has to finish the binary($k) before it can do the print "below\n". And that binary() call has its own above/binary()/below sequence to follow. It would be like saying:

print "x\n"; print "y\n"; print "z\n";

You wouldn't expect that to output "x/z/y", would you? Each line has to finish before the next one starts, and the recursive binary() call will (from the top) also involve another recursive call before it returns to the original call.

It may be simpler to look a version without the calculations, just showing the call path:

# How many steps we want to take my $steps = 3; sub recur { my ($x) = @_; # Create a number of leading spaces equal to our current $x my $ldr = " " x $x; # Note that we're starting print "${ldr}Start with $x\n"; # We only go to $steps to make it a smallish example if($x >= $steps) { print "${ldr}At $steps, returning.\n"; return; } # Recurse with the next number. Say we're doing it, do it, say we +'re # done. my $y = $x + 1; print "${ldr}-> Subcall with $y\n"; recur($y); print "${ldr}<- Back from $y\n"; # And now we're done with this number, so step back print "${ldr}Done with $x\n"; } # Start off recur(0);

So now we're just calling ourselves $steps times, and noting where we go. Output:

% ./tst.pl Start with 0 -> Subcall with 1 Start with 1 -> Subcall with 2 Start with 2 -> Subcall with 3 Start with 3 At 3, returning. <- Back from 3 Done with 2 <- Back from 2 Done with 1 <- Back from 1 Done with 0

So what happens? We start off by calling recur() with 0. So that says it's doing so, then re-calls itself with 1. That says it's doing so, re-calls with 2. Says it's doing so, re-calls with 3.

Now the 3 notices that's our limit, and so it returns. It returns back to the re-call that had 2, which now completes, and returns. It returns back to the re-call that had 1, which now completes, and returns. It returns back to the original call with 0, which returns. And then it falls off the end of the script.


In reply to Re: recursion basics by fullermd
in thread recursion basics by wrinkles

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-16 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found