Mailing List Archive

Is perl tail recursive? (fwd)
------- start of forwarded message -------
Path: csnews!boulder!agate!howland.reston.ans.net!newsfeed.internetmci.com!in1.uu.net!sun.sirius.com!usenet
From: "Peter Seibel" <seibel@sirius.com>
Newsgroups: comp.lang.perl.misc
Subject: Is perl tail recursive?
Date: 09 Dec 1995 15:15:03 -0800
Organization: Self
Lines: 67
Sender: seibel@lily.sirius.com
Message-ID: <m2pwdx6d8o.fsf@lily.sirius.com>
Reply-To: seibel@sirius.com
NNTP-Posting-Host: ppp010-sf1.sirius.com
Full-Name: Peter Seibel
X-Newsreader: Gnus v5.0.12

As the subject asks, is perl tail recursive?

I suspect not since Script 1 below eats up more and more memory while
Scripts 2 and 3 do not. But I don't have a particularly deep
understanding of tail recursion so I could well be confused. (My
understanding is that all three would take up the same amount of
memory the same if perl were tail recursive.)

Script 1:
#!/usr/bin/perl
use strict;
&foo;
sub foo { my @foo = (0..10000); &foo; }
__END__

Script 2:
#!/usr/bin/perl
use strict;
&foo;
sub foo { @::foo = (0..10000); &foo; }

Script 3:
#!/usr/bin/perl
use strict;
my @foo;
&foo;
sub foo { @foo = (0..10000); &foo; }

For bonus points, maybe someone can tell me how to reclaim some
memory. I tried the following variation on the above:

#!/usr/bin/perl
use strict;
my @foo;
&foo(\@foo);
sub foo
{
my ($list) = @_;
@$list = $::counter == 30 ? () : (0..10000);
$::counter++;
&foo($list);
}
__END__

It didn't increase the amount of memory used over time but neither did
it ever decrease the amount of memory (as I sort of hoped it would
once I chopped @foo down to the empty list.) And, if instead of:

@$list = $::counter == 30 ? () : (0..10000);

I said:

$::counter == 30 and undef @$list;
defined @$list and @$list == (0 .. 10000);

It just ate up more and more memory. What's up with that? Basically, I
have a program that makes some big temporary lists and I want to get
the memory back when I'm done with the list.

-Peter

--
Peter Seibel seibel@mojones.com
Writer/Perl Hacker 510-482-9105

"I wish I had been born with more foresight."
-- Alex Heatley
------- end of forwarded message -------
--
Tom Christiansen Perl Consultant, Gamer, Hiker tchrist@mox.perl.com
I believe in the waterbed theory of linguistics.
If you push down here, it pops up there.
-- <1993Jan20.181244.8680@netlabs.com>