View Full Version : Perl --> Ackermann Function
danbaron
23-06-2010, 07:47
[font=courier new][size=8pt]Here is a program that does exactly the same thing as the thinBasic script here:
http://community.thinbasic.com/index.php?topic=3403.0
Only, this one is written in Perl.
:oops: :x :grrrr:
Dan
#------------------------------------------------------------
sub ack1
{
my ($m, $n) = @_;
if(($m < 0) or ($n < 0)){return -1;}
if($m == 0){return $n + 1;}
if($n == 0){return ack1($m - 1, 1);}
return ack1($m - 1, ack1($m, $n - 1));
}
#------------------------------------------------------------
sub ack2
{
my @stack = ();
my ($m, $n) = @_;
if(($m < 0) or ($n < 0)){return -1;}
while(1)
{
if($m == 0)
{
if(@stack == 0){return $n + 1;}
else{$m = shift(@stack); $n += 1; redo;}
}
if($n == 0)
{
$m -= 1;
$n = 1;
redo;
}
unshift(@stack, $m - 1);
$n -= 1;
}
}
#------------------------------------------------------------
print " ack1 ack2\n";
print "\n";
for(my $i = 0; $i <= 3; $i++)
{
for(my $j = 0; $j <= 7; $j++)
{
printf "ack(%1d, %1d) = %04d %04d\n", $i, $j, ack1($i, $j), ack2($i, $j);
}
}
printf "ack(%1d, %1d) = %04d %04d\n", 4, 0, ack1(4, 0), ack2(4, 0);
print "\n";
print "Done.\n";
print "Press a key.\n";
my $dump = readline(STDIN);
#------------------------------------------------------------
Hi Dan,
Have you tried perl6 , it looks very interesting, yes it is very very very slow but it has great features, i begin to study its tutorials, please allow me to post something about it for who may find it interesting:
here: http://rakudo.org the announcement that in 29 july they will release version 1 of an official perl 6 release called "rakudo star". they call it a "usable perl6". after 10 years of experimental releases.
for those who want to play with it, but an experimental very slow release, the windows binaries are here ( the windows binaries are available 1 month after releasing the source code):
http://sourceforge.net/projects/parrotwin32/files/
just download two files:
1-setup-parrot-2.5.0.exe 8 MB
2-setup-parrot-2.5.0-rakudo-30.exe 1.4 MB
install them to the same folder c:\Parrot-2.5.0 or you may get errors,
be sure the path are set to c:\Parrot-2.5.0\bin , now in notepad write the following self explanatory code , save it to filename.pl of beter file.p6 and from console execute: perl6.exe filename.p6:
my @numbers = (1, 2, 3);
my @new = (5, 3);
if (all(@new) > all(@numbers)) {
say "all bigger";
}
if (any(@new) > all(@numbers)) {
say "there is at least one bigger";
}
another interesting self explanatory example (notice the conditional):
print "hello\n";
say "hello world";
"good morning".say ;
my $a = prompt "Type in a number which is a triple of numbers between 3 and 8: ";
while $a != any(3 .. 8) * 3
{$a = prompt "again Type in a triple of 3 ... 8: ";}
my $small = prompt "Type another number between 0 and $a: ";
my $big = prompt "Type another number between $a and 100: ";
if 0 <= $small <= $a <= $big <= 100 {
say "good";
} else {
say "something is fishy";
}
look the syntax while $a != any(3 .. 8 ) * 3 means: while $a not equal any of 3*3, 4*3, 5*3, 6*3, 7*3, 8*3
notice the if conditional also can have parentheses: if (0 <= $small <= $a <= $big) <= 100 or without parentheses.
there is a tutorial book:
http://github.com/perl6/book/downloads
also an Introduction to Perl6 here http://szabgab.com/talks/perl6/ but it is outdated, not all examples are correct; but it is usefull.
the developers said they will optimize the code in the future and thier main interest is to implement the ideas presented by larry wall the original author of the classic old perl, which he said once:
"True greatness is measured by how much freedom you give to others, not by how much you can coerce others to do what you want." --Larry Wall
finally the ackerman function: in the rossetta code:
http://rosettacode.org/wiki/Ackermann_function#Perl_6 there is this:
sub A(Int $m, Int $n) {
$m == 0 ?? $n + 1 !!
$n == 0 ?? A($m - 1, 1 ) !!
A($m - 1, A($m, $n - 1));
}
say A(3,2);
but for values such as A(3,7) it is heating my cpu, and may stay forever, to exit press ctrl-c ,we need to wait for the promised perl6 star version.
at last more examples such as euler problems can be found here:
http://github.com/perl6/perl6-examples
danbaron
24-06-2010, 09:33
[font=courier new][size=8pt]Hi Zak.
I had stopped following Perl 6. I came to believe, that it would be released, - never. Now, I see that you are
correct, July 29.
Perl website.
http://www.perl.org/
Parrot bytecode compiler.
http://parrot.org/
Perl 5 books. "Programming Perl", by Larry Wall and two others, is a really nice book. Even a person who knew nothing
about programming might like reading it. (Perl people generally seem to be friendly, maybe because of Larry Wall.)
http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dstripbooks&field-keywords=perl&x=0&y=0
Perl 6 web site.
http://dev.perl.org/perl6/
More Perl 6.
http://www.perl6.org/getting-started/
Preliminary version of Perl 6 book.
http://cloud.github.com/downloads/perl6/book/book-2010-06.pdf
Article about transitioning from Perl 5 to Perl 6.
http://perlgeek.de/en/article/5-to-6
CPAN, "Comprehensive Perl Archive Network".
http://www.cpan.org/
PDL, "Perl Data Language"
http://pdl.perl.org/
"Padre", the Perl IDE.
http://padre.perlide.org/
I think that many people are unaware of how big, and what a complete language that Perl 5, has become. Also, unlike any
other computer language I know about, it is smart enough to evaluate data, according to the "context", in which the data
is being used. I think there are five contexts - scalar, list, boolean, void, and interpolative. So, because Perl is
aware of contexts, it requires the programmer to do less work, code becomes more like human speech. I think that Perl
has been continually developed, for over 22 years. And now, it seems, Perl 6 is coming. One reason I keep going back to
Perl, is because, there seems to be no end to what new things you can learn about it. To me, by comparison, Python is a
very simple language, there's not that much there. I think that, studying a programming language, can be like reading a
novel. If the story is short and simple, you finish it quickly. If the story is long, complicated, and has surprises,
you remain interested for a long time.
:oops: :x :grrrr:
Dan
ErosOlmi
02-07-2010, 09:36
"True greatness is measured by how much freedom you give to others, not by how much you can coerce others to do what you want." --Larry Wall
I strongly believe on that.
Sometimes it is not easy to follow it at 100% but I try.
Sometimes are the people to which you refer that are not ready to have all that freedom :D
I think that, studying a programming language, can be like reading a
novel. If the story is short and simple, you finish it quickly. If the story is long, complicated, and has surprises,
you remain interested for a long time.
This one is not bad too :D
LanceGary
02-07-2010, 11:18
[
I think that, studying a programming language, can be like reading a
novel. If the story is short and simple, you finish it quickly. If the story is long, complicated, and has surprises,
you remain interested for a long time.
This one is not bad too :D
Hmm. It sounds good - computer languages as literature. But is it true? Surely the purpose of computer languages is to provide a medium for thought rather than an object of study. If one is forever having to study the language before writing any code is it a good language?
Lance
ErosOlmi
02-07-2010, 13:10
It can be interpreted more like: if the language does not evolve and change, it start to loose user appeal