Proposed new syntax

Steve D'Aprano steve+python at pearwood.info
Sat Aug 19 00:15:22 EDT 2017


On Sat, 19 Aug 2017 12:59 am, Chris Angelico wrote:

> On Fri, Aug 18, 2017 at 11:46 PM, Rustom Mody <rustompmody at gmail.com> wrote:
>> Compare the well-known haskell tutorial
>> http://learnyouahaskell.com/starting-out
>> whose comprehension intro starts:
>>
>> | If you've ever taken a course in mathematics, you've probably run into set
>> | comprehensions. They're normally used for building more specific sets out
>> | of general sets. A basic comprehension for a set that contains the first
>> | ten even | natural numbers is
>>
>> | S = {2·x | x ∈ ℕ, x ≤ 10}

For the record, this is not the best example to give, since the Natural numbers
ℕ are not well-defined. Some people include 0, and some do not, so there's a
slight ambiguity to the above.

http://mathworld.wolfram.com/NaturalNumber.html

Despite that nit-pick, set builder notation is very common in maths, but not
universal. It is taught in secondary education (high school) in Australia, but
not to all students.



>> Analogous thing shown at ghci prompt:
>>
>> | ghci> [x*2 | x <- [1..10]]
>> | [2,4,6,8,10,12,14,16,18,20]
> 
> And what if you HAVEN'T taken a course in mathematics? What use is
> this then? How would you teach this to a non-mathematician?

Speaking as someone who *has* taken a course of mathematics or two, I find that
Rustom's insistence in coming back to the fundamentals of set theory and the
Zermelo–Fraenkel axioms is not terribly helpful. Even among professional
mathematicians. Z-F and the axiom of choice and related matters are extremely
specialised and abstract fields of little interest to the average working
mathematician.

In my experience, they're of more interest to philosophers and dilettantes than
actual mathematicians, outside of the minority working in that specific field.

Yes yes, it is absolutely fundamental to mathematics, just as quantum mechanics
is absolutely fundamental to an understanding of matter. How many bridge
builders care about quantum mechanics?

Python is not Haskell and makes no pretence at being mathematically sound[1].
The Zen of Python sets forth some of the design principles in the language,
and "mathematical purity" is not one of them.

The opposite, in fact: "practicality beats purity."

To answer your (Chris') question:

When I teach comprehension syntax, I always mention set builder notation and
say "you may have been taught this is school". I don't think I have ever come
across someone who both *was* taught it and *remembers* so, but I'll keep
trying. For those who don't understand set builder notation (so far, everyone
I've tried to teach comps to) I explain them in terms of for loops.

In fact, even if you do understand set builder notation, for more complex
examples with "if" clauses and multiple "for" loops, I maintain that you have
to think of it in terms of loops to understand it. I am extremely skeptical
that anyone could look at a comprehension like:

    [expr for x in A for y in B if P for z in C if Q for w in D for v in E if R]

and understand it *without* converting it to nested loops.


> Pretty much everyone, at some point in their lives, will follow a set
> of written instructions. Most commonly, a recipe for some sort of
> food. It consists of a set of ingredients and a sequence of commands.
> This translates well into a classic imperative style

Indeed. People find imperative (recipe) algorithms easy to follow, and pure
functional reasoning hard. I'm glad that functional programming is fashionable
again, and hope that people will learn good habits from it, but I think that
mathematical purity is not necessary or even helpful in the majority of
programming tasks.

I expect that languages like Haskell, like Scheme and Lisp, will be greatly
influential but remain niche languages themselves.





[1] Assuming that mathematics actually is sound, which thanks to Gödel we know
is unprovable.

-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list