So I have been hanging out at #perl6 and working with autrijus and the group on PUGS. Mostly I have been writing tests, but recently I decided to whip up some code examples to test my new found perl 6 knowledge.
Now I have always been a fan of functional languages, and one of my favorite features of functional languages is argument pattern matching. So I decided to try and reproduce some of the classic examples of argument pattern matching using the multi-sub feature of perl 6.
Here is the Standard ML code I based these examples off of.
And the Perl 6 code ...fun sum [ ] = 0 | sum (h::t) = h + (sum t); fun length [ ] = 0 | length (h::t) = 1 + (length t);
And the equivalent (sorta) Perl 5 code ...multi sub sum () returns Int { 0 } multi sub sum (*$x, *@xs) returns Int { $x + sum(@xs) } multi sub length () returns Int { 0 } multi sub length (*$x, *@xs) returns Int { 1 + length(@xs) }
These examples are quite simple, but the full example code has a few more complex examples in it.sub sum { return 0 unless @_; my ($h, @t) = @_; return $h + sum(@t); } sub length { return 0 unless @_; shift; return 1 + length(@_); }
I encourage anyone who is doubting the future of perl 6 to download the latest PUGS and give it a try. Even though it is still a little rough around the edges, it is progressing quite nicely and has a number of new and interesting language features already implemented.
And of course we can always use more tests and examples.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Functional Perl 6/PUGS
by bunnyman (Hermit) on Feb 28, 2005 at 15:08 UTC | |
by stvn (Monsignor) on Feb 28, 2005 at 15:39 UTC | |
by BrowserUk (Patriarch) on Feb 28, 2005 at 16:08 UTC | |
by stvn (Monsignor) on Feb 28, 2005 at 17:15 UTC | |
by BrowserUk (Patriarch) on Feb 28, 2005 at 18:38 UTC | |
by stvn (Monsignor) on Feb 28, 2005 at 20:14 UTC | |
| |
|
Re: Functional Perl 6/PUGS
by rg0now (Chaplain) on Feb 28, 2005 at 15:53 UTC | |
by stvn (Monsignor) on Feb 28, 2005 at 16:57 UTC | |
by rg0now (Chaplain) on Feb 28, 2005 at 23:41 UTC | |
by stvn (Monsignor) on Mar 01, 2005 at 01:18 UTC | |
by fergal (Chaplain) on Mar 01, 2005 at 15:20 UTC | |
by tilly (Archbishop) on Mar 05, 2005 at 02:38 UTC |