Proposed new syntax
Steve D'Aprano
steve+python at pearwood.info
Sun Aug 13 10:36:02 EDT 2017
On Sat, 12 Aug 2017 01:02 am, Ian Kelly wrote:
> On Thu, Aug 10, 2017 at 11:45 PM, Steve D'Aprano
> <steve+python at pearwood.info> wrote:
>> Comprehension syntax makes the sequential loop explicit: the loop is right
>> there in the syntax:
>>
>> [expr for x in iterable]
>
> This is a peculiarity of Python.
Yes? We're talking about Python? I haven't accidentally been posting to
comp.lang.anything-but-python have I? *wink*
I am not referring to syntax from other languages. (One wonders what a list
comprehension in Whitespace would look like...) We're talking about Python,
which prefers explicit English-like executable pseudo-code over implicit
cryptic mathematical symbolic expressions.
Python prefers to be explicit, rather than implicit: that's why purely
functional idioms like map, filter and reduce are less idiomatic than
comprehensions.
But for the record, this is not a peculiarity of Python by any means. I count at
least a dozen other languages which use an explicit "for" in their
comprehension syntax:
Boo, Ceylon, Clojure, CoffeeScript, Common Lisp, Cobra, Elixir, F#, Mozilla's
Javascript, Julia, Perl6, Racket and Scala.
https://en.wikipedia.org/wiki/Comparison_of_programming_languages_%28list_comprehension%29
And very possibly the first language with comprehensions, SETL (from 1969),
used "forall":
[n in [2..N] | forall m in {2..n - 1} | n mod m > 0]
https://en.wikipedia.org/wiki/List_comprehension#History
> Here's a list comprehension in
> Haskell, which has supported them since version 1.0 in 1990, much
> longer than Python:
>
> [x * 2 | x <- L, x * x > 3]
Sure. In Haskell, comprehensions are *implicit* loops, rather than explicit like
in Python.
Python's comprehensions are inspired by Haskell's, but we made different choices
than they did: we make the fact that a comprehension is a loop over values
explicit, rather than implicit, and we use words instead of cryptic symbols.
--
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