New module, Sub::Lambda

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Hi, I wrote two modules that try to provide syntactic sugar for
lambdas in Perl.

The standard syntax for lambdas is to use subrefs, like this:

sub { do_something_with($_[0]) }

This works just fine, however it gets ugly for curried functions.

One way to solve the problem is with a function fn, which Sub::Lambda

 (fn a => fn b => '$a+$b')->(1)->(2)      #=3
 (fn qw(a b) => '$a+$b')->(1, 2)            #= 3
 (fn qw(h -t) => '')->(1,2,3,4)  #= {1 => [2,3,4]}

Together with ap for application, you can take it as far as defining
the applicative-order Y combinator:

*Y = fn m => ap(
(fn f => ap m => fn a => ap qw(f f a)) =>
(fn f => ap m => fn a => ap qw(f f a))

The second, more experimental module, Sub::Lambda::Filter, uses source
filtering to extend Perl syntax with Haskell'ish lambdas.

With that, Y looks more like it should:

use Sub::Lambda::Filter;
my $fac = (\f -> \n -> { ($n<1) ? 1 : $n*$f->($n-1) });
my $Y   = (\m -> (\f -> m (\a -> f f a)) (\f -> m (\a -> f f a)));
print $Y->($fac)->(5) . "\n";  # 120 = 5!

There are yet some issues with Sub::Lambda::Filter as it takes a few
shortcuts to correctly parsing the Perl code it filters. I have not
found a ready Perl parser in Perl, is there one?

Let me know what you think.

I did the upload to CPAN but the module is not appearing yet - maybe
it's the cache, or maybe I messed something up as it's my first CPAN
upload; you can now check out both  module here: /

Or, if you have darcs,

darcs get



Site Timeline