Perl: the Markov chain saw PerlMonks

### Comment on

 Need Help??
Hello,

In order to use goto to obtain true tail recursion, you need to use goto LABEL and not goto SUB because the later does just what the regular call does. Here is one way how you might do your factorial example:

```printf "10 factorial is %d", scheme_procs('factorial', 1, 10);

my \$ls = mklist(1,5,5,7,3,6); # make a list

print pair_str(\$ls) . "\n"; # print it

my \$max = scheme_procs('max', \$ls); # get the max

printf "Max is \$max\n";

#pair_push(\$ls,\$ls); # make a circular list!!
# now do the bad thing, never terminate but monitor
# memory usage
#\$max = scheme_procs('max', \$ls); # get the max
#printf "Max is \$max\n";

###################################################
# call scheme_procs(proc name, args);
###################################################
sub scheme_procs{
my \$proc = shift;
if(\$proc eq 'max'){ # go to the correct label
goto SCM_MAX;
} elsif(\$proc eq 'factorial'){
goto SCM_FACTORIAL;
} else {
die "no such proc: \$proc";
}

SCHEME_RP:  # return the return values
if(wantarray){
return(@_);
} else {
return \$_[0];
}

SCM_FACTORIAL:
my (\$total,\$n) = @_;
if(\$n == 0){
@_ = (\$total);
goto SCHEME_RP; # return
} else {
@_ = (\$total*\$n, \$n - 1);
goto SCM_FACTORIAL; # tail call
}

SCM_MAX:
my \$p = shift;
print_resources();
if(null(\$p)){
die "Error";
}
if(null(cdr(\$p))){
@_ = (car(\$p));
goto SCHEME_RP; # return
} else {
if(car(\$p) > (car(cdr(\$p)))){
@_ = (cons(car(\$p), cdr(cdr(\$p))));
goto SCM_MAX; # tail call
} else {
@_ = (cdr(\$p));
goto SCM_MAX; # tail call
}
}
}

# helper functions for our simplistic scheme subsystem
sub cons{ my(\$a,\$d) = @_; [\$a,\$d] }
sub car{ shift->[0] }
sub cdr{ shift->[1] }
sub null{ not defined shift }
sub set_cdr{ my (\$ls, \$v) = @_;  \$ls->[1] = \$v }
sub mklist{
return unless @_;
return cons(shift, mklist(@_));
}
sub pair_str{
my \$p = shift;
if(ref \$p eq 'ARRAY'){
"(" . pair_str(car(\$p)) . " " . pair_str(cdr(\$p)) . ")";
} else {
if(null(\$p)){
"()"
} else {
\$p;
}
}
}

# this is a really bad resource monitor but works to monitor
# memory growth to check for proper tail recursion
sub print_resources{
my @lines = grep {\$_ =~ /\s+(\d+)/ and \$1 eq \$\$}  `ps u`;
print @lines;
}

In reply to Re: Pure Perl tail call optimization by abstracts
in thread Pure Perl tail call optimization by pdcawley

Title:
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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.

Create A New User
Chatterbox?
 [Eily]: can't the nodelet be moved though? Maybe you could put one that doesn't change first ("Sections" or "Find Nodes" for example) [Eily]: "Other Users" seems like a poor choice :P [Eily]: nope, Nodelet Settings doesn't let you move the XP Nodelet, CSS might [marinersk]: That would mitigate the distraction/jangle issue, but then the information wouldn't be easy to find when it is populated. Plus, I don't currently see a way to move it, but I'm not done poking around on that point yet.

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2017-05-29 14:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (192 votes). Check out past polls.