Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^8: 99 Problems in Perl6

by gaal (Parson)
on Dec 15, 2006 at 23:44 UTC ( #590156=note: print w/ replies, xml ) Need Help??


in reply to Re^7: 99 Problems in Perl6
in thread 99 Problems in Perl6

Didn't we end up using when for guards? The problem with multis for this I seem to remember was that it was hard to spec their order if they're defined in separate compilation units.


Comment on Re^8: 99 Problems in Perl6
Download Code
Re^9: 99 Problems in Perl6
by TimToady (Parson) on Dec 15, 2006 at 23:51 UTC
    We'll support matching against sigs using when if you really want ordered guards, but multis are defined to ignore ordering, so it doesn't matter where they're defined, in this file or in any other file. Only the type semantics of the signature matter. Overuse of ordered guards is actually kind of a semantic flaw in Haskell, I think. It's a way of sneaking in sequential dependencies without a monad.
      Pure variants like this one are desugared into a case:

      compress x = case x of [] -> [] [a] -> [a] -- singleton list (x:y:xs) -> (if x == y then [] else [x]) ++ compress (y:xs)

      And can do no monadic monkey business because compress is pure. In this they are simply a more convenient way of spelling out some branches. You wouldn't say if is a sneaky way of doing sequential dependencies, would you?

      Sure, with a monadic function you can also have pattern guards that do monadic stuff, but I don't think you can bind without noticing, and in any case the function type will tell you it's monadic.

      Ordered variants in Haskell let you do things like

      funky (x:y:xs) = ... -- I'm guaranteed to have two elements or more funky (x:xs) = ... {- This pattern would have matched a long list, b +ut since the previous variant came first, we know the l +ist is of length 1 or 2. * -}

      This is incredibly useful sometimes. Okay, when I want when I know where to find it. :-)

      * For folks not familiar with Haskell who count three or two items in the two patterns and don't see why I'm talking of lists of at least two and one or two elements respectively: in Haskell, "(a:b)" means a is an element and b is a list of zero or more elements. That's why by convention you see names like "xs" and "ys", pronounced "exes" and "whys", though there's nothing in the language to enforce names like that. The expression (x:y:xs) means (x:(y:xs)).

        I agree with you here that ordered matching of sigs is convenient, even though it isn't as clear.

        Standard ML guarantees ordered matching in functions (and also in lambdas and case expressions), so you can say this:

        fun fact 0 = 1 | fact n = n * fact(n - 1);
        (this is not the best way to define factorial though).

        Prolog also matches the heads of predicate definitions in order.

        Mathematica is a bit different. It matches argument lists to more specific patterns first, and when it is not clear which pattern is more specific, it matches them in order.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2014-10-20 04:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (72 votes), past polls