For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > March 2008 > New module, Sub::Lambda









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author New module, Sub::Lambda
Антон Таяновский

2008-03-18, 7:01 pm

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
provides:

(fn a => fn b => '$a+$b')->(1)->(2) #=3
(fn qw(a b) => '$a+$b')->(1, 2) #= 3
(fn qw(h -t) => '{$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:

http://ha4.fajno.net/dev/Perl/CPAN/Sub-Lambda/

Or, if you have darcs,

darcs get http://ha4.fajno.net/dev



Cheers,

--Anton

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com