develooper Front page | perl.perl6.language | Postings from September 2005

FYI: Lambda Calculus on Perl 6

Thread Next
From:
Dan Kogai
Date:
September 4, 2005 20:35
Subject:
FYI: Lambda Calculus on Perl 6
Message ID:
A77477E9-C139-48D2-86C2-CE60842FA659@dan.co.jp
Folks,

I recently needed to write a series of codes on lambda calculus in  
perl.  As MJD has shown Perl 5 can handle lambda calculus but I am  
beginning to get tired of whole bunch of 'my $x = shift' needed.


   our $ZERO =
       sub { my $f = shift;
           sub { my $x = shift; $x }};
   our $SUCC =
       sub { my $n = shift;
             sub { my $f = shift;
                   sub { my $x = shift;
                         $f->($n->($f)($x)) }}};
   our $ADD =
       sub{ my $m = shift;
            sub { my $n = shift;
                  sub { my $f = shift;
                        sub { my $x = shift;
                              $m->($f)($n->($f)($x)) }}}};
   our $MULT =
       sub { my $m = shift;
             sub { my $n = shift;
                   sub { my $f = shift;
                         $m->($n->($f)) }}};
   our $POW =
       sub { my $m = shift;
             sub { my $n = shift;
                   $n->($m) }};

And I found that these can be made much, much simpler and more  
intuitive with Perl 6, even more so than scheme!

   our $ZERO = sub($f){ sub($x){ $x }};
   our $SUCC = sub($n){ sub($f){ sub($x){ $f.($n.($f)($x)) }}};
   our $ADD  = sub($m){ sub($n){ sub($f){ sub($x){ $m.($f)($n.($f) 
($x)) }}}};
   our $MULT = sub($m){ sub($n){ sub($f){ $m.($n.($f)) }}};
   our $POW  = sub($m){ sub($n){ $n.($m) }};

You can even make it simpler by removing dots but I leave it that way  
because it looks more like the original notation that way (i.e.   
zero := λf.λx.x).

Runs perfectly fine on Pugs 6.2.8.  Add the code below and see it for  
yourself.

   my $one     = $SUCC.($ZERO);
   my $two     = $SUCC.($one);
   my $four    = $ADD.($two)($two);
   my $eight   = $MULT.($two)($four);
   my $sixteen = $POW.($four)($two);
   for($one, $two, $four, $eight, $sixteen) -> $n {
       $n.(sub($i){ 1 + $i})(0).say
   };

Maybe we can use this for advocacy.

Dan the Perl 6 User Now

P.S.  I am surprised to find Pugs does not include this kind of  
sample scripts.
  
Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About