List comprehensions: Difference between revisions
Content added Content deleted
(→{{header|Python}}: Three equivalent formulations of list comprehension as a use of the list monad) |
|||
Line 1,826: | Line 1,826: | ||
More generally, the list comprehension syntax can be understood as a concise syntactic sugaring of a use of the list monad, in which non-matches are returned as empty lists, matches are wrapped as single-item lists, and concatenation flattens the output, eliminating the empty lists. |
More generally, the list comprehension syntax can be understood as a concise syntactic sugaring of a use of the list monad, in which non-matches are returned as empty lists, matches are wrapped as single-item lists, and concatenation flattens the output, eliminating the empty lists. |
||
The monadic 'bind' operator for lists is concatMap, traditionally used with its first two arguments flipped. The following three formulations of a |
The monadic 'bind' operator for lists is concatMap, traditionally used with its first two arguments flipped. The following three formulations of a '''pts''' (pythagorean triangles) function are equivalent: |
||
<lang python>from functools import (reduce) |
<lang python>from functools import (reduce) |