[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Re: Neat or horrid?



Ariel Scolnicov writes:
> Does it do proper tail recursion?  If not, I'd try
> 
>      sub sum {@_ ? shift () + goto &sum : 0}
> 
> except that it doesn't work (5.005_03).  I don't think I understand
> why not, though.

goto is not how you do recursion.  goto is like exec -- it replaces,
rather than nests.  So you're dropping the existing subroutine call
sum(1,2,3,4,5) and replacing it with sum(2,3,4,5), which is then
replaced with sum(3,4,5) then with sum(4,5) then with sum(5) then with
sum() which returns 0, so that's what sum(1,2,3,4,5) appears to
return.

Nat

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe