[Python-ideas] ML Style Pattern Matching for Python
spir
denis.spir at gmail.com
Sat Dec 18 15:07:38 CET 2010
On Sat, 18 Dec 2010 14:31:12 +0100
Eike Welk <eike.welk at gmx.net> wrote:
> Great that you thought about a syntax for dicts and sets.
>
>
> One other point that must be solved, is how to distinguish symbols that should
> be assigned, from symbols that are specify conditions. Ocamls solution is that
> all symbols are assigned. Conditions must be specified with literals. Here is
> an example to illustrate it:
>
>
> This is a simple function that compares its argument "x" to the integer one:
>
> def foo(x):
> match x with
> | 1 ->
> print("x is equal to 1")
> | _ ->
> print("x is not equal to 1")
>
>
> In the code below we want to parametrize the value that we use for comparison.
> The parameter "a" is introduced for this purpose. Now we need a way to tell
> the compiler that "x" should be compared to "a"; and not assigned to "a". In
> Ocaml this is impossible. The compiler would issue a warning ("Redundant case
> in a pattern matching.").
>
> def foo(x, a):
> match x with
> | a ->
> print("x is equal to %s" % a)
> | _ ->
> print("x is not equal to %s" % a)
>
> Maybe you come up with a nice notation for this problem.
No, I would do it like in OCaml, I think (but may be wrong) it fits the python-way, like having no formally qualified readonly or private slots.
> By the way, why does no one like those "|" characters? They look good and take
> up few space!
;-) It does not fit Python overall syntax look. And requires a magic id '_' for default case. And requires adding '->'. And we already have 'case x:' (and 'else') which fit perfectly. And are far more readable (no need to guess what given symbols mean, including the 'sense' of an arrow ;-).
> Eike.
Denis
-- -- -- -- -- -- --
vit esse estrany ☣
spir.wikidot.com
More information about the Python-ideas
mailing list