mandog has asked for the wisdom of the Perl Monks concerning the following question:

In this node started by Anonymous Monk the majority opinion seemed to be that recursion was at best a party trick. At least according to the friendly folks in the average CS 102 class, recursion is a good thing. (Unless your recursion forms a chain the way the original poster’s did).

In the example below, recursion forms a bushy tree and works just fine. It is arguably easier to follow than a looping solution and is for all practical purposes just as fast.

The previous version of MS VC++ created executables with stack space of 4M. My 1987 data structure text observes that most modern computers like the PDP-11 (!) have a special stack register that makes sub routine calls relatively cheap.

Can somebody could come up with a looping solution to this problem (guess a number between two other numbers as quickly as possible) that was is as easy to follow and uses significantly less time or memory?

The world has many people who can either code better than I or who remember their school days better. I suspect it can be done...

use strict; use warnings; use diagnostics; # explain the warnings ! # guesses integet between two other integers sub recuGuess($$){ my($low,$high)=@_; my $mid=int (($low+$high)/2); print "is $mid low, high or correct? :"; my $ans=<STDIN>; $ans=uc(substr($ans,0,1)); if('C' eq $ans){ print "thank you for playing\n"; }elsif('L' eq $ans){ &recuGuess($mid+1,$high); }elsif('H' eq $ans){ &recuGuess($low,$mid-1); }else{ print "bad input\n"; &recuGuess($low,$high); } } recuGuess(1,40000);


--mandog

Replies are listed 'Best First'.
(tye)Re: bushy recursion a good thing ?
by tye (Sage) on Sep 15, 2001 at 09:31 UTC

    Yes, recursion is often (even usually) easier to understand and easier to implement correctly on the first try. Non-recursive solutions are likely to only be a little bit faster. But, recursive solutions are likely to fail on much "smaller data" and prevent you from providing certain types of interfaces compared to non-recursive solutions. So I prefer non-recursive solutions if I suspect that efficiency will be unusually important, I want to succeed with as large of data as possible, or I want to provide the best possible API (such as if I'm writing code for public reuse).

    I go over this in a bit more detail here: (tye)Re: Recursion. The rest of that thread may also be of interest.

    Also, one thing I didn't mention before: People sometimes turn to recursive solutions when a simple loop (not even requiring a stack) would make more sense (as in the thread you link to). In such cases, the recursive solution is likely to be harder to understand and to get right on the first try. So, if you find yourself contemplating a recursive solution, you should at least think a bit about how you might solve it without recursion. (:

            - tye (but my friends call me "Tye")
Re: bushy recursion a good thing ?
by John M. Dlugosz (Monsignor) on Sep 15, 2001 at 01:28 UTC
    Yes, because your recursion is strictly "tail end". So replace the recurive call with a statement to reset the parameters and jump back to the top. E.g. $low=$mid+1; redo; with the whole thing in a do.

    An optomizing compiler would see "return foo(args)" within the same function foo and do just that for you. In languages like Lisp and Prolog, it's critical for the implementation.

Re: bushy recursion a good thing ?
by blakem (Monsignor) on Sep 15, 2001 at 01:32 UTC
    How about:
    #!/usr/bin/perl -wT use strict; my $low = 0; my $high = 40000; # guesses integet between two other integers my $ans = ''; while ($ans ne 'C') { my $mid=int (($low+$high)/2); print "is $mid low, high or correct? :"; $ans=<STDIN>; $ans=uc(substr($ans,0,1)); if('L' eq $ans){ $low = $mid+1; }elsif('H' eq $ans){ $high = $mid-1; }elsif($ans ne 'C') { print "bad input\n"; } } print "thank you for playing\n";

    -Blake

(dkubb) Re: (1) bushy recursion a good thing ?
by dkubb (Deacon) on Sep 15, 2001 at 02:53 UTC

    The following does what you want without recursion:

    #!/usr/bin/perl -w use strict; use constant MIN => 0; use constant MAX => 40000; my($low, $high, $mid) = (MIN, MAX); print "Think of a number between $low and $high\n"; while(1) { $mid = int(($low + $high) / 2); last if $low >= $high; print "How about $mid? Is it (l)ow, (h)igh or (c)orrect? : "; my $answer = lc(substr <STDIN>, 0, 1); $answer eq 'c' ? last : $answer eq 'l' ? $low = $mid + 1 : $answer eq 'h' ? $high = $mid - 1 : print "bad input\n"; } print "The number you are thinking of is $mid!\n";