Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

dankogai (3580)

dankogai
  (email not shown publicly)

Journal of dankogai (3580)

Sunday September 04, 2005
10:38 PM

Lambda Calculus on Perl6

[ #26596 ]
(also posted on p6l)
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.