Supriya Ghosh (Editor)

Comparison of programming languages (list comprehension)

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

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.

Contents

Boo

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

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

C#

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

Clojure

An infinite lazy sequence:

A list comprehension using multiple generators:

Common Lisp

List comprehensions can be expressed with the loop macro's collect keyword. Conditionals are expressed with if, as follows:

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.

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.

Haskell

An example of a list comprehension using multiple generators:

Io

By using Range object, Io language can create list as easy as in other languages:

ISLISP

List comprehensions can be expressed with the for special form. Conditionals are expressed with if, as follows:

JavaScript

Borrowing from Python, JavaScript has array comprehensions. Although this feature has been proposed for inclusion in the sixth edition ECMAScript standard, Mozilla is the only implementation that currently supports it.

Julia

Julia supports comprehensions using the syntax:

and multidimensional comprehensions like:

Mythryl

s = [ 2*i for i in 1..100 where i*i > 3 ];

Multiple generators:

pyth = [ (x,y,z) for x in 1..20 for y in x..20 for z in y..20 where x*x + y*y == z*z ];

OCaml

OCaml supports List comprehension through OCaml Batteries.

Perl 6

my @s = ($_ * 2 if $_ ** 2 > 3 for 0 .. 99);

Python

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).

Racket

An example with multiple generators:

Scala

Using the for-comprehension:

Scheme

List comprehensions are supported in Scheme through the use of the SRFI-42 library.

An example of a list comprehension using multiple generators:

Visual Prolog

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

Windows PowerShell

which is short-hand notation of:

References

Comparison of programming languages (list comprehension) Wikipedia