Further information: Boo for .NET–Mono
List with all the doubles from 0 to 10 (exclusive)
List with the names of the customers based in Rio de Janeiro
Further information: C# 3.0 Language Integrated Query
The previous code is syntactic sugar for the following code written using lambda expressions:
Further information: Ceylon, Ceylon comprehensions
Filtering numbers divisible by 3:
Multiple "generators":
Further information: List comprehension § Clojure
An infinite lazy sequence:
A list comprehension using multiple generators:
Further information: CoffeeScript
Further information: Common Lisp
List comprehensions can be expressed with the loop macro's collect keyword. Conditionals are expressed with if, as follows:
Further information: Cobra
List the names of customers:
List the customers with balances:
List the names of customers with balances:
The general forms:
Note that by putting the condition and expression after the variable name and enumerable object, editors and IDEs can provide autocompletion on the members of the variable.
Further information: Dart, Dart lists
Further information: Elixir
Further information: Erlang
Further information: F#
Lazily-evaluated sequences:
Or, for floating point values
Lists and arrays:
List comprehensions are the part of a greater family of language constructs called computation expressions.
Further information: Haskell
An example of a list comprehension using multiple generators:
Further information: Io
By using Range object, Io language can create list as easy as in other languages:
Further information: ISLISP
List comprehensions can be expressed with the for special form. Conditionals are expressed with if, as follows:
Further information: Julia
Julia supports comprehensions using the syntax:
and multidimensional comprehensions like:
It is also possible to add a condition:
And just changing square brackets to the round one, we get a generator:
Multiple generators:
Further information: Nemerle
Nim has built-in seq, set, table and object comprehensions on the sugar standard library module:1
The comprehension is implemented as a macro that is expanded at compile time, you can see the expanded code using the expandMacro compiler option:
The comprehensions can be nested and multi-line:
Further information: OCaml
OCaml supports List comprehension through OCaml Batteries.2
Further information: Perl
Array with all the doubles from 1 to 9 inclusive:
Array with the names of the customers based in Rio de Janeiro (from array of hashes):
Further information: PowerShell
which is short-hand notation of:
Further information: Python syntax and semantics § Comprehensions
Python uses the following syntax to express list comprehensions over finite lists:
A generator expression may be used in Python versions >= 2.4 which gives lazy evaluation over its input, and can be used with generators to iterate over 'infinite' input such as the count generator function which returns successive integers:
(Subsequent use of the generator expression will determine when to stop generating values).
Further information: R
Further information: Racket
An example with multiple generators:
Further information: Raku (programming language)
Further information: Scala
Using the for-comprehension:
Further information: Scheme
List comprehensions are supported in Scheme through the use of the SRFI-42 library.3
Further information: SETL
Further information: Smalltalk
Further information: Visual Prolog
Nim sugar module https://nim-lang.github.io/Nim/sugar.html#collect.m%2Cuntyped%2Cuntyped ↩
OCaml Batteries Included http://batteries.forge.ocamlcore.org/ ↩
Scheme SRFI 42: Eager Comprehensions http://srfi.schemers.org/srfi-42/srfi-42.html ↩