kragen@pobox.com (Kragen Sitaker) writes: > Ariel Scolnicov wrote: > > Neat, but not as neat as primality testing... > > > > 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. > > It's not tail-recursive --- the continuation for the call to &sum is > (in Scheme) (lambda (val) (+ (shift) val)), not (lambda (val) val), > which is what it would need to be for proper tail-recursion. > > You could do something like > > #!/usr/bin/perl -w > use strict; > sub sum { if ($#_) { unshift @_, shift() + shift; goto &sum } else { $_[0] }} > print sum (37), "\n"; > print sum (48, 31, 11), "\n"; > > That's properly tail-recursive, but I don't think it's properly Fun. Thanks for the many people who posted, setting me straight. Thanks especially to Kragen, who took the trouble to post the Right Thing. -- Ariel Scolnicov |"GCAAGAATTGAACTGTAG" | ariels@compugen.co.il Compugen Ltd. |Tel: +972-2-6795059 (Jerusalem) \ We recycle all our Hz 72 Pinhas Rosen St. |Tel: +972-3-7658514 (Main office)`--------------------- Tel-Aviv 69512, ISRAEL |Fax: +972-3-7658555 http://3w.compugen.co.il/~ariels ==== Want to unsubscribe from Fun With Perl? Well, if you insist... ==== Send email to <fwp-request@technofile.org> with message _body_ ==== unsubscribe