I wrote: > Now the pattern matching is more interesting, but again, I'd need to > see a proposed syntax for Python before I could begin to consider it. > If I understand it properly, pattern matching in Haskell relies > primarily on Haskell's excellent typing system, which is absent in > Python.
Nick Coghlan replies: > There's no real need for special syntax in Python - an appropriate tuple > subclass will do the trick quite nicely: [... sample code matching tuples ...] Aha, but now you've answered my question about syntax, and I can see that your syntax lacks most of the power of Haskell's pattern matching. First of all, it can only match tuples ... most things in Python are NOT tuples. Secondly (as Michael Walter explained) it doesn't allow name binding to parts of the pattern. Honestly, while I understand that pattern matching is extremely powerful, I don't see how to apply it in Python. We have powerful introspective abilities, which seems to be helpful, but on the other hand we lack types, which are typically a key feature of such matching. And then there's the fact that many of the elegent uses of pattern matching use recursion to traverse data structures... a no-no in a CPython that lacks tail-recursion elimination. There is one exception... matching strings. There we have a powerful means of specifying patterns (regular expressions), and a multi-way branch based on the content of a string is a common situation. A new way to write this: s = get_some_string_value() if s == '': continue; elif re.match('#.*$', s): handle_comment() elif s == 'DEFINE': handle_define() elif s == 'UNDEF': handle_undefine() elif re.match('[A-Za-z][A-Za-z0-9]*$', s): handle_identifier() else: syntax_error() would be might be nice, but I can't figure out how to make it work more efficiently than the simple if-elif-else structure, nor an elegent syntax. -- Michael Chermside _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com