Welcome to the Monastery PerlMonks

### Re^10: 99 Problems in Perl6

by gaal (Parson)
 on Dec 16, 2006 at 09:01 UTC ( #590182=note: print w/replies, xml ) Need Help??

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

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)).

Replies are listed 'Best First'.
Re^11: 99 Problems in Perl6
by ambrus (Abbot) on Dec 17, 2006 at 10:23 UTC

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.

Similar syntax exists in Haskell and is called a guard.

```-- Here the |s should be read as one vertical line, like a mathematica
+l function def.
fact n      | n == 0    = 1
| otherwise = n * fact (n - 1)

-- Here's the same thing in different style, the same one as I'd used
+earlier and
-- called "declarative", sometimes called direct pattern matching, bec
+ause each
-- variant gets to bind whatever it managed to match.
fact 0 = 1
fact n = n * fact (n - 1)

There are plenty of non-syntactic variations on this theme. The scanl one is my favorite.

Create A New User
Node Status?
node history
Node Type: note [id://590182]
help
Chatterbox?
 [erix]: haha [LanX]: Èrnem rulez!

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2018-04-24 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (85 votes). Check out past polls.

Notices?