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.

fun sum [ ] = 0 | sum (h::t) = h + (sum t); fun length [ ] = 0 | length (h::t) = 1 + (length t);
And the Perl 6 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) }
And the equivalent (sorta) Perl 5 code ...
sub sum { return 0 unless @_; my ($h, @t) = @_; return $h + sum(@t); } sub length { return 0 unless @_; shift; return 1 + length(@_); }
These examples are quite simple, but the full example code has a few more complex examples in it.

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.

-stvn

In reply to Functional Perl 6/PUGS by stvn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.