Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: rough approximation to pattern matching using local

by Athanasius (Archbishop)
on Jan 25, 2015 at 06:51 UTC ( [id://1114416]=note: print w/replies, xml ) Need Help??


in reply to rough approximation to pattern matching using local

Hello gregory-nisbet,

First, I don’t understand what you are trying to do. How does “pattern matching in languages like Haskell and OCaml” differ from Perl-style regular expressions work? Some explanation would be helpful here. At the very least, you should indicate the output you want to get from the call to match "bob", 45, sub { print $bob; }.

Second, the warning messages are telling you that the package-global variable $bob is being used without having been first initialised or even declared. A search through your code shows that $bob occurs once only, in the print statement within the anonymous subroutine passed to sub match. There is also the string, "bob", passed in to that subroutine, so I’m guessing you expect the variable to be somehow created, from the passed-in string, within that sub? It isn’t, and I don’t know why you think the while loop localisation you identify is relevant here?

Third, when you start sprinkling no strict 'refs' and no strict around your code to keep the compiler quiet, this is almost always a sign that your design is faulty.

Fourth, the use of prototypes in sub match ($$$) is probably a bad idea. Please study

Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen

and then restrict yourself to using prototypes in only those situations where the “implicit context coercion” (in Tom Christiansen’s phrase) which they provide is actually useful.

Update: Amended sentence re “pattern matching”. Thanks to LanX, below.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: rough approximation to pattern matching using local (Multi Subs)
by LanX (Saint) on Jan 25, 2015 at 09:43 UTC
    > How does “pattern matching in langauges like Haskell and OCaml” differ from Perl-style regular expressions?

    It's an unfortunate doubling of terminology.

    In Haskell you are free to "redefine“ a functions body multiple times for different argument "constellations"

    Those args have to "match a pattern", not (necessarily) a regular expressions!

    IIRC does Perl 6 have something very similar just named differently... ehm ...

    Multi-subs ? ? ?

    Cheers Rolf

    PS: Je suis Charlie!

    update

    yes it's called multi-subs in Perl 6, but from the examples I saw so far, I'd say that Haskell is more powerful.

    update

    see also http://en.wikibooks.org/wiki/Haskell/Pattern_matching

    Note

    Pattern matching on what?

    Some languages like Perl and Python use the term pattern matching for matching regular expressions against strings. The pattern matching we refer to in Haskell is something completely different. In fact, you're probably best off forgetting what you know about pattern matching for now

        Does it work (now)? If so, in which of the disparate implementations? (A (direct) download link appreciated.)

        What penalty does it impose over two (or more), separate (differently named) subroutines?

        Ie. Is the syntactic sugar worth the implementation costs?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
        Unpacking is nice and it's good to see this feature not only in Python...

        But I'm afraid you are missing the point here.

        "pattern matching" a la Haskell allows to filter for literal arguments not only types like demonstrated for multi subs.

        For instance a naive Fibonacci (in pseudo-code) can look like this

        Multi fib(0|1) = { 1 }; Multi fib($i) = { fib($i-1) + fib($i-2) };

        Can I do this with multi subs in Perl 6?

        Cheers Rolf

        PS: Je suis Charlie!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-16 03:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found