Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

There is a reason why pattern matching syntactic sugar is not in python. Pattern matching is very powerful, but it is also ambiguous. And it is impossible to come up with good looking syntax, unfortunately.

Ambiguous, because in the:

    # y = 5
    #
    match x with:
        1: "my string"
        2|3:  print "2,3"
        y: print y
there is a problem. Because, really, in case of 'y' you want to be able to do both: bind variables; use already defined variables. And there is no way you can do both with clean and concise syntax. On the other hand, there is already a way to achieve the same results with if,elif,else. That's why (sadly) pattern matching is not there.


I don't get the issue. In OCaml you can do

  |y -> y + x
If y was defined in the outside scope, the case scope just shadows it.


I think, he wants to match the case when x == y. You can do that too.

let y = 5 in

match x with 1 -> print "my string" | 2 | 3 -> print "2,3" | z when z = y -> print y


What I was saying, is that if you are to complain about lack of pattern matching syntax in Python, you want to consider that such syntax require concise and unambiguous handling of a number of cases:

matching a value of variable:

    |z when z = y -> print y
free matching with a bind:

    |y -> print y
matching several values:

    |2|3 -> print "2 or 3"
matching tuples:

    |2,3 -> print "tuple 2,3"

Now, if I'm trying to come up with such syntax for Python, I see immediate problems. For example, lets consider following syntax for matching several values:

    match x with:
        1: print "my string"
        2,3: print "2,3"
 
Would it be equivalent to matching a tuple (2,3) or matching one of the values 2 or 3? Ambiguous. So you need some other syntax. Let's try a few variations:

    match x with:
        1: print "my string"
        2:3: print "2 or 3"         # plain ugly
        2|3: print "2 or 3"         # interferes with '|' op
        in (2,3): print "2 or 3"    # too complicated 
        x in (2,3): print "2 or 3"  # elif was simpler 
Or consider:

    match x with:
        1: print "my string"
        y: print y
Would that be matching a value of variable (|z when z = y -> print y), or free matching with a bind (|y -> print y)?

Having experimented with that a bit, I have a feeling, that it's just impossible to make up 'match' syntax for Python that would fit into the language. And I think that's why it is not there. And why we don't even want match syntax there. Now, of course I'd be happy to change my opinion, if somebody would rise up to the challenge and come up with some syntax that fits.


Yes, if you would only allow binding it resolves the conflict. But, if a five year old would look at the code:

    y = 5
    match x with:
        1: print '1'
        y: print '5'
he will think it is equivalent to:

    y = 5
    if x == 1: print '1'
    elif x == y: print '5'
And that would not be true.


This problem is also present in Haskell and it seems to work out pretty well there


That.

Every language solves that with a desambiguation rule. In haskell, the first matching line runs. It's no worse than the way C, Java, and family solve the if-else ambiguity, for example.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: