# Comparison of programming languages (list comprehension)

### From Wikipedia, the free encyclopedia

This article needs additional citations for verification. Please help improve this article by adding reliable references (ideally, using inline citations). Unsourced material may be challenged and removed. (February 2009) |

## [edit] List Comprehensions

List comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It follows the form of the mathematical *set-builder notation* (*set comprehension*.) as distinct from the use of map and filter functions.

### [edit] Boo

List with all the doubles from 0 to 10 (exclusive)

doubles = [i*2 for i in range(10)]

List with the names of the customers based on Rio de Janeiro

rjCustomers = [customer.Name for customer in customers if customer.State == "RJ"]

### [edit] C#

var ns = from x in Enumerable.Range(0,100) where x*x > 3 select x*2;

The previous code is syntactic sugar for the following code written using lambda expressions:

var ns = Enumerable.Range(0, 100) .Where(x => x*x > 3) .Select(x => x*2);

### [edit] Clojure

An infinite lazy sequence:

(for [x (iterate inc 0) :when (> (* x x) 3)] (* 2 x))

A list comprehension using multiple generators:

(for [x (range 20), y (range 20), z (range 20) :when (== (+ (* x x) (* y y)) (* z z))] [x y z])

### [edit] Common Lisp

List comprehensions can be expressed with the `loop`

macro's `collect`

keyword. Conditionals are expressed with `if`

, as follows:

(loop for x from 0 to 100 if (> (* x x) 3) collect (* 2 x))

### [edit] Erlang

L = lists:seq(0,100). S = [2*X || X <- L, X*X > 3].

### [edit] F#

{ for x in 0 .. 100 when x*x > 3 -> 2*x }

Or, more correctly for floating point values

{ for x in 0.0 .. 100.0 when x**2.0 > 3.0 -> 2.0*x }

### [edit] Haskell

An example of a list comprehension using multiple generators:

pyth = [(x,y,z) | x <- [1..20], y <- [x..20], z <- [y..20], x^2 + y^2 == z^2]

### [edit] JavaScript

Borrowing from Python, JavaScript 1.7 and later have array comprehensions.^{[1]} Although this feature has been proposed for inclusion in the fourth edition ECMAScript standard, Mozilla is the only implementation that currently supports it.

/* There is no "range" function in JavaScript's standard library, so the application must provide it. */ function range(n) { for (var i = 0; i < n; i++) yield i; } [2 * x for (x in range(100)) if (x * x > 3)]

JavaScript 1.8 adds Python-like generator expressions.

### [edit] Nemerle

$[x*2 | x in [0 .. 100], x*x > 3]

### [edit] Perl

List comprehensions are supported in Perl through the use of the *List::Comprehensions* CPAN module.^{[2]}

### [edit] Python

The Python programming language uses the following syntax for expressing list comprehensions over finite lists:

S = [2*x for x in range(100) if x**2 > 3]

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:

from itertools import count S = (2*x for x in count() if x**2 > 3)

(Subsequent use of the generator expression will determine when to stop generating values).

### [edit] R

x = (0:100) S = 2 * x[x ^ 2 > 3]

### [edit] Scala

Using the for-comprehension:

val s = for (x <- Stream.from(0); if x*x > 3) yield 2*x

### [edit] Visual Prolog

S = [ 2*X || X = list::getMember_nd(L), X*X > 3 ]

### [edit] Windows PowerShell

$s = ( 0..100 | ? {$_*$_ -gt 3} | % {2*$_} )

## [edit] References

**^**New in JavaScript 1.7, Mozilla Developer Center**^**List::Comprehensions CPAN module