<?xml version="1.0" encoding="windows-1252"?>
<node id="402234" title="Re^5: Recursion: the Towers of Hanoi problem" created="2004-10-25 10:38:30" updated="2005-06-14 00:43:13">
<type id="11">
note</type>
<author id="188181">
abitkin</author>
<data>
<field name="doctext">
&lt;i&gt;Strangely, using my &lt;code&gt;$l=$_[3]--&lt;/code&gt; and using &lt;code&gt;a(@_[0,2,1,3])&lt;/code&gt; 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.&lt;/i&gt;&lt;P&gt;
from perlsub:&lt;BR /&gt;
&lt;i&gt;The array @_ is a local array, but its elements are aliases for the actual scalar parameters.&lt;/i&gt;&lt;P&gt;
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.&lt;P&gt;
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.)&lt;p&gt;
Third, and this is where it gets interesting, when removing all the whitespace I can I get (for your solution):
&lt;code&gt;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;&lt;/code&gt;
Which just happens to be the same amount of space as (my solution):
&lt;code&gt;sub a{my$l=pop;a(@_[0,2,1],--$l)."Move disc $l from $_[0] to $_[2]
".a(@_[1,0,2],$l)if$l&gt;0;}print a 'A'..'C',pop;&lt;/code&gt;
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.&lt;P&gt;
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.&lt;P&gt;
EDIT:&lt;BR /&gt;
perlsub not perlvar&lt;P&gt;
EDIT2:&lt;BR /&gt;
pop instead of shift to remove chars&lt;P&gt;
EDIT3 (10/26/04):&lt;BR /&gt;
Best I've done, modifing jasper's code, is 111 chars.&lt;BR /&gt;
&lt;code&gt;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;&lt;/code&gt;
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-188181"&gt;
&lt;BR&gt;==
&lt;BR&gt;Kwyjibo. A big, dumb, balding North American ape. With no chin.
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
400359</field>
<field name="parent_node">
401251</field>
</data>
</node>
