2 ms·
"I have a feature that I am sort of jealous of because it's appearing in more and more other languages: pattern matching. And I cannot come up with the right ke
by DonHopkins 8y ago
"I have a feature that I am sort of jealous of because it's appearing in more and more other languages: pattern matching. And I cannot come up with the right keyword, because all the interesting keywords are already very popular method names for other forms of pattern matching." -Guido van Rossum
- nikofeyn 8y agois he just now becoming aware of it? it seems he had no idea something like sml existed when he first created python. i am sure he had heard of lisp/scheme but probably ignored it.
- coldtea 8y agoThank god, else we wouldn't have Python but another ML or Lisp variant that nobody (statistically speaking) uses...
- DonHopkins 8y agoAnd all this time I thought R was the programming language that statistically speaking people used.
- nikofeyn 8y agoi don't understand your snark. do you honestly think python, as a programming language, is superior to something like racket or f#? these languages are newer than python of course, but the point is that they are representative of what we could be using.
- coldtea 8y ago>i don't understand your snark It's simple, it was added to counter-balance the smug ascertainment of Guido's ignorance, and the taken-for-granted assumption that we'd be so much better if he modeled Python more after ML/Lisp/etc. >do you honestly think python, as a programming language, is superior to something like racket or f#? No, I think the subject is moot. Python has been adopted and is widely successful for being Python. We had MLs and Schemes/Lisps available before and after Python and they didn't manage to get traction. Why do you think having the original Python be more like these would help its case? Even if the result was a "better language" in the sense you think Racket or F# are, those are still niche, and there's all the reasons to believe Python would be too if it followed their model. So, it's not "if only Python was ML/Lisp like we would now have an equally successful but better Python!". It's more likely "if Python was ML/Lisp it would still be niche today, and we wouldn't have a language with the characteristics that people love in Python and adopted it for, but yet another ML or Lisp version".
- zimablue 8y agoNote that python does have pattern matching, just not to the same degree as some languages. (a, (b, *c, d), *e) = [1, (2, 3, 4, 5), 6, 7] Good for nested iterables, but not supporting dict/object unpacking.
- tomp 8y agoNot sure that's pattern matching... I'd call that destructuring, for me pattern matching also means the ability to fail the match (and try another).
- arethuza 8y agoPresumably Prolog's unification being one of the most powerful 'pattern matching' mechanisms? http://www.dai.ed.ac.uk/groups/ssp/bookpages/quickprolog/node12.html http://www.dai.ed.ac.uk/groups/ssp/bookpages/quickprolog/nod... [Amused to see that the page I linked to there is over 20 years old!]
- vorg 8y ago> all the interesting keywords are already very popular method names for other forms of pattern matching When I first read that, I thought he meant run-time lookups of overloaded method names, which is a form of pattern matching on parameter lists.
- DonHopkins 8y agoYou can do multiple dispatching in Python, by hand or with decorators, but it can get kind of awkward, not as elegant as CLOS's and Dylan's approach with generic functions. https://www.artima.com/weblogs/viewpost.jsp?thread=101605 https://www.artima.com/weblogs/viewpost.jsp?thread=101605 Five-minute Multimethods in Python, by Guido van van Rossum. I used to believe that multimethods were so advanced I would never need them. Well, maybe I still believe that, but here's a quick and dirty implementation of multimethods so you can see for yourself. Some assembly required; advanced functionality left as an exercise for the reader. https://python-3-patterns-idioms-test.readthedocs.io/en/latest/MultipleDispatching.html https://python-3-patterns-idioms-test.readthedocs.io/en/late... https://pythonhosted.org/multidispatch/ https://pythonhosted.org/multidispatch/ https://adambard.com/blog/implementing-multimethods-in-python/ https://adambard.com/blog/implementing-multimethods-in-pytho... https://opendylan.org/documentation/intro-dylan/multiple-dispatch.html https://opendylan.org/documentation/intro-dylan/multiple-dis... https://opendylan.org/documentation/intro-dylan/objects.html#generic-functions-objects https://opendylan.org/documentation/intro-dylan/objects.html... https://en.wikipedia.org/wiki/Multiple_dispatch https://en.wikipedia.org/wiki/Multiple_dispatch https://en.wikipedia.org/wiki/Generic_function https://en.wikipedia.org/wiki/Generic_function
- pmontra 8y agoI don't think it's a matter of keywords. I'm working with both Elixir and Python on different projects right now. Pattern matching is very deeply ingrained into Elixir. Two examples to scratch the surface This is pattern matching in function arguments def process_file({:ok, {file_name, file_content}}) do ... :ok end def process_file({:error, reason}) do ... :error end def process_file(_), do: error This is pattern matching in assignment {:ok, content} = File.read(file_name) Actually the = character is the pattern matching operator, which accidentally ends up assigning values in some cases: it can't assign to the :ok atom but it can assign to the content variable. If File.read returns {:error, "some error"}, the match will fail and the program will stop. Usually I get a notice from Sentry and a supervisor process will start it again, possibly running into the same problem or not. Sometimes errors are caused by external services that have occasional hiccups. Some changes are so structural that turn languages into another one and I'm not sure it's worth it. We have so many languages to choose from.
- andreareina 8y agoI can't find a reference to what parser python uses internally, but if it does at least one token lookahead, wouldn't you be able to disambiguate special syntax from any standard expression since the former allows tokens separated by only a space and the latter doesn't? e.g. afaik `match some_call():` is a syntax error, so you don't need to worry about match being a legal identifier. I daresay another issue is going to be the syntax -- the naïve way of implementing it nests two blocks: one for the pattern-matched block as a whole, and and another for the actual cases, which means that now you've got a double indent. IIRC this was a reason he gave against implementing case statements way back when. There is one construct that's almost like a block but doesn't indent code, decorators. It's rich enough to implement multiple dispatch in functions, including pattern-matching on values and not just on types. Unfortunately it won't get you the sort of things you could do with actual blocks, e.g. access to other variables.