Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Parameter Pattern Matching in Perl

by withering (Monk)
on Feb 10, 2014 at 13:03 UTC ( [id://1074239]=perlmeditation: print w/replies, xml ) Need Help??

Hello fellow monks.

A few days ago I was working on type inference in both simply-typed lambda calculus and Hindley-Milner System. While I was trying to use hashtables to implement branches for getting rid of annoying 'if's, I realized that there's no quick ways to emulate pattern matching in ML or Haskell -- It is a fact that we could not easily do that due to the dynamic nature of Perl's type system. However, I managed to emulate some of the features I used in Erlang, ML, or perhaps Haskell and made a small CPAN module HOI::Match (where HOI means Higher-Order Imperative):

HOI::Match

With the module, I am able to write such a sum function:

sub sum { HOI::Match::pmatch( "h :: r" => sub { my %args = @_; $args{h} + sum($args{r}) }, "nil" => sub { 0 } )->(@_) }

(Note that the arguments in @_ may be FETCH several times so it is dangerous to rely on side effects of FETCH on arguments.)

Any suggestion or criticism is welcomed. I am looking forward for your replies.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://1074239]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2026-01-19 15:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (123 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.