Proposed new syntax

Ian Kelly ian.g.kelly at gmail.com
Mon Aug 14 11:33:33 EDT 2017


On Sun, Aug 13, 2017 at 8:36 AM, Steve D'Aprano
<steve+python at pearwood.info> wrote:
> 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.

With respect, your own post that I was responding to discussed
"traditional functional programming idioms". Why are traditional
map/filter on the table for discussion but not traditional list
comprehensions?

In any case, this thread started with the question of what we would
expect a particular list comprehension syntax to look like. While
existing Python syntax is certainly important to understanding that
(and that was the basis for my own initial response stemming from the
knowledge that Python maps fors and ifs to nested blocks and I would
expect while to work in the same way), for many Python users their
understanding of list comprehensions may not necessarily be rooted in
Python syntax, and so I think it is also important to consider how
list comprehensions are viewed in other contexts.

> 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:

Yes, as I noted in my reply.

> Boo, Ceylon, Clojure, CoffeeScript, Common Lisp, Cobra, Elixir, F#, Mozilla's
> Javascript, Julia, Perl6, Racket and Scala.

I think some of these take influence from Python. Common Lisp is a bit
of an oddball; it doesn't actually have list comprehensions as a
distinct syntax. The example given there is just a standard loop using
the "collect" keyword.

The Javascript example should be removed from that page. The syntax
didn't make it into the standard and Mozilla's own documentation has a
big red warning saying not to use it. They'll probably eventually
remove it from Gecko.

> Sure. In Haskell, comprehensions are *implicit* loops, rather than explicit like
> in Python.

No, they aren't. Haskell list comprehensions use lazy evaluation.
Here's an example of an infinite list comprehension:

Prelude> let squares = [ x ** 2 | x <- [1 ..] ] :: [Float]
Prelude> :print squares
squares = (_t1::[Float])

You might say that this is more like a generator expression but the
result is in fact a list. We can evaluate the first four elements:

Prelude> print $ take 4 squares
[1.0,4.0,9.0,16.0]

And then see that these have been lazily evaluated in the list:

Prelude> :print squares
squares = 1.0 : 4.0 : 9.0 : 16.0 : (_t2::[Float])

A Haskell list comprehension is not a loop at all.



More information about the Python-list mailing list