<div dir="auto"><div>Calculating the Ackermann function as Knuth up-arrows really has little practical user. The first few values are well known, the rest won't be calculated before the heat death of the universe.<br><br><div class="gmail_quote"><div dir="ltr">On Thu, May 3, 2018, 2:02 PM Chris Angelico <<a href="mailto:rosuav@gmail.com" target="_blank" rel="noreferrer">rosuav@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Fri, May 4, 2018 at 3:18 AM, Ed Kellett <<a href="mailto:e%2Bpython-ideas@kellett.im" rel="noreferrer noreferrer" target="_blank">e+python-ideas@kellett.im</a>> wrote:<br>
> I believe the intention in the example you quoted is syntax something like:<br>
><br>
>     <match-case> ::= <pattern><br>
>                    | <pattern> "if" <expression><br>
><br>
> where the expression is a guard expression evaluated in the context of<br>
> the matched pattern.<br>
><br>
> IOW, it could be written like this, too:<br>
><br>
> number = match x:<br>
>     1 if True => "one"<br>
>     y if isinstance(y, str) => f'The string is {y}'<br>
>     _ if True => "anything"<br>
><br>
> I do see a lot of room for bikeshedding around the specific spelling.<br>
> I'm going to try to resist the temptation ;)<br>
<br>
Okay, let me try to tease apart your example.<br>
<br>
1) A literal matches anything that compares equal to that value.<br>
2) A name matches anything at all, and binds it to that name.<br>
2a) An underscore matches anything at all. It's just a name, and<br>
follows a common convention.<br>
3) "if cond" modifies the prior match; if the condition evaluates as<br>
falsey, the match does not match.<br>
4) As evidenced below, a comma-separated list of comparisons matches a<br>
tuple with as many elements, and each element must match.<br>
<br>
Ultimately, this has to be a series of conditions, so this is<br>
effectively a syntax for an elif tree as an expression.<br>
<br>
For another example, here's a way to use inequalities to pick a<br>
numeric formatting:<br>
<br>
display = match number:<br>
    x if x < 1e3: f"{number}"<br>
    x if x < 1e6: f"{number/1e3} thousand"<br>
    x if x < 1e9: f"** {number/1e6} million **"<br>
    x if x < 1e12: f"an incredible {number/1e9} billion"<br>
    _: "way WAY too many"<br>
<br>
I guarantee you that people are going to ask for this to be spelled<br>
simply "< 1e3" instead of having the "x if x" part. :)<br>
<br>
> How about this?<br>
><br>
> def hyperop(n, a, b):<br>
>     return match (n, a, b):<br>
>         (0, _, b) => b + 1<br>
>         (1, a, 0) => a<br>
>         (2, _, 0) => 0<br>
>         (_, _, 0) => 1<br>
>         (n, a, b) => hyperop(n-1, a, hyperop(n, a, b-1))<br>
><br>
> versus:<br>
><br>
> def hyperop(n, a, b):<br>
>     if n == 0:<br>
>         return b + 1<br>
>     if n == 1 and b == 0:<br>
>         return a<br>
>     if n == 2 and b == 0:<br>
>         return 0<br>
>     if b == 0:<br>
>         return 1<br>
>     return hyperop(n-1, a, hyperop(n, a, b-1))<br>
<br>
I have no idea what this is actually doing, and it looks like a port<br>
of Haskell code. I'd want to rewrite it as a 'while' loop with maybe<br>
one level of recursion in it, instead of two. (Zero would be better,<br>
but I think that's not possible. Maybe?) Is this something that you do<br>
a lot of? Is the tuple (n, a, b) meaningful as a whole, or are the<br>
three values independently of interest?<br>
<br>
Not sure how this is useful without a lot more context.<br>
<br>
ChrisA<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" rel="noreferrer noreferrer" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer noreferrer noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div></div></div>