Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Strangely, using my $l=$_[3]-- and using a(@_[0,2,1,3]) etc. doesn't work at all. @_ is not localised ($_3 ends up very negative and the solution becomes deeply recursive), which makes me wonder if the above solution works. I haven't compared the output to the original solution's output.

from perlsub:
The array @_ is a local array, but its elements are aliases for the actual scalar parameters.

Basically, if you think about it, @_ has to be localized. Otherwise if you called a function inside a function (something I hope most of us do) your @_ array would be wiped out, leaving you with the parameters you sent into the child function you just called. The particular behavior your seeing is probably becuase the @_ array contains aliases to scalars.

Second, using 0 based discs is okay, it's easy enough for the user to add 1 in their head (or to start thinking like a cs person.)

Third, and this is where it gets interesting, when removing all the whitespace I can I get (for your solution):

sub a{if(my$l=pop){a(@_[0,2,1],$l-1);print"Move disc $l from $_[0] to +$_[2] ";a(@_[1,0,2],$l-1);}}a 'A'..'C',pop;
Which just happens to be the same amount of space as (my solution):
sub a{my$l=pop;a(@_[0,2,1],--$l)."Move disc $l from $_[0] to $_[2] ".a(@_[1,0,2],$l)if$l>0;}print a 'A'..'C',pop;
so it seems that both solutions are (atleast as far as this little experiment goes) equal. My goal is to squeeze everything that I have now down to 115 or 110 by Tuesday.

Oh and I have compared our outputs, and with the exception of the disc number (mine being one less than yours) they are exactly the same.

perlsub not perlvar

pop instead of shift to remove chars

EDIT3 (10/26/04):
Best I've done, modifing jasper's code, is 111 chars.

sub a{if(my$l=pop){a(@_[0,2,1],--$l);print"Move disc $l from $_[0] to +$_[2] ";a(@_[1,0,2],$l);}}a 'A'..'C',pop;

Kwyjibo. A big, dumb, balding North American ape. With no chin.

In reply to Re^5: Recursion: the Towers of Hanoi problem by abitkin
in thread Recursion: The Towers of Hanoi problem by DigitalKitty

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (5)
    As of 2018-01-22 20:52 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (237 votes). Check out past polls.