Professional Documents
Culture Documents
htm
List comprehension
From Wikipedia, the free encyclopedia
Contents
1 Overview
2 History
3 Examples in different programming languages
3.1 B-Prolog
3.2 Clojure
3.3 Common Lisp
3.4 Erlang
3.5 F#
3.6 Falcon
3.7 Haskell
3.8 JavaFX
3.9 Javascript 1.8
3.10 Mythryl
3.11 OCaml
3.12 Pure
3.13 Python
3.14 Scala
3.15 Visual Prolog
4 Similar constructs
4.1 Monad comprehension
4.2 Set comprehension
4.3 Dict comprehension
4.4 Parallel list comprehension
4.5 XQuery and XPath
4.6 LINQ in C#
5 See also
6 Notes and references
6.1 Haskell
6.2 OCaml
6.3 Python
6.4 Common Lisp
6.5 Clojure
6.6 Axiom
7 External links
Overview
Consider the following example in set builder notation.
The order of generation of members of the output list is based on the order of
items in the input.
Here, the list [0..] represents , x^2>3 represents the predicate, and 2*x
represents the output expression.
List comprehensions give results in a defined order (unlike the members of sets);
and list comprehensions may generate the members of a list in order, rather than
produce the entirety of the list thus allowing, for example, the previous Haskell
definition of the members of an infinite list.
History
The SETL programming language (later 1960s) had a set formation construct,
and the computer algebra system AXIOM (1973) has a similar construct that
processes streams, but the first use of the term "comprehension" for such
constructs was in Rod Burstall and John Darlington's description of their
functional programming language NPL from 1977.
Although the original example denotes an infinite list, few languages can express
that, so in some of those cases we show how to take a subset of {0,1,...,100}
rather than a subset of .
B-Prolog
Clojure
Common Lisp
or something similar.
An infinite lazy sequence can be created in a variety of ways, such as the CLOS
object system or a yield macro.
Erlang
F#
Further information: F#
Generators are of the form [for x in collection do ... yield expr] for lists
and seq {for x in collection do ... yield expr} for sequences. For
example:
> seq { for x in 0..100 do
if x*x > 3 then yield 2*x } ;;
val it : seq<int> = seq [4; 6; 8; 10; ...]
Falcon
The "comp" generic method family provides wide support for comprehension. For
example, the "mfcomp" method can be applied to an array:
Falcon can also use functional generators to provide input lists. For example, the
following code uses a continuation to create a set of pairs.
Method "comp" was introduced in version 0.9.6, and methods "mcomp" and
"mfcomp" in version 0.9.6.2.
Haskell
JavaFX
Javascript 1.8
Mythryl
OCaml
OCaml Batteries Included has uniform comprehension syntax for lists, arrays,
enumerations (like streams), lazy lists (like lists but evaluated on-demand), sets,
hashtables, etc.
For instance,
# [? 2 * x | x <- 0 -- max_int ; x * x > 3 ?];;
- : int Enum.t = <abstr>
etc.
Pure
Python
M = [[1, 2, 3], [4,5,6], [7,8,9]] # This is an example of a nested list, (a list inside a list).
col2 = [row[1] for row in M] # The logic goes like this: since it starts from 0, not 1, row[1]
# selects the second part of each list. (2, 5, and 8.)
List comprehension in Python speeds up the process of doing certain tasks, like
getting certain numbers out of another list. (Or any other data type that can be
looped through).
Scala
Visual Prolog
Similar constructs
Monad comprehension
Set comprehension
Dict comprehension
Like the original NPL use, these are fundamentally database access languages.
/library/book//paragraph[@style='first-in-chapter']
In XQuery, full XPath is available, but FLWOR statements are also used, which is
[5]
a more powerful comprehension construct.
for $b in //book
where $b[@pages < 400]
order by $b//title
return
<shortBook>
<title>{$b//title}</title>
<firstPara>{($book//paragraph)[1]}</firstPara>
</shortBook>
Here the XPath //book is evaluated to create a sequence (aka list); the where
clause is a functional "filter", the order by sorts the result, and the
<shortBook>...</shortBook> XML snippet is actually an anonymous function
that builds/transforms XML for each element in the sequence using the 'map'
approach found in other functional languages.
map(
newXML(shortBook, newXML(title, $1.title), newXML(firstPara, $1...))
filter(
lt($1.pages, 400),
xpath(//book)
)
)
LINQ in C#
C# 3.0 has a group of related features called LINQ, which defines a set of query
operators built to manipulate list-like structures (object enumerations, SQL
datasets, …) which can be used in ways similar to list comprehension:
var s = from x in Enumerable.Range(0, 100) where x*x > 3 select x*2;
See also
The SELECT statement together with its FROM and WHERE clauses in SQL
Programming language
Mathematical notation
Monads in functional programming for monads and monadic notation in
general
For other programming language constructs used to process sequences:
Generator (computer science)
Map (higher-order function)
For other programming language constructs copied from the mathematical
notation:
Guard (computing)
Pattern matching
Operator (programming)
Haskell
/onlinereport/exps.html#list-comprehensions) .
The Glorious Glasgow Haskell Compilation System User's Guide, chapter
7.3.4 Parallel List Comprehensions (http://www.haskell.org/ghc/docs/latest
/html/users_guide/syntax-extns.html#parallel-list-comprehensions) .
The Hugs 98 User's Guide, chapter 5.1.2 Parallel list comprehensions (a.k.a.
zip-comprehensions) (http://cvs.haskell.org/Hugs/pages/users_guide/hugs-
ghc.html#ZIP-COMPREHENSION) .
OCaml
Python
Common Lisp
Clojure
Axiom
External links
SQL-like set operations with list comprehension one-liners in the Python
Cookbook (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe
/159974)
Discussion on list comprehensions in Scheme and related constructs
(http://lambda-the-ultimate.org/classic/message11326.html)
List Comprehensions across languages (http://langexplr.blogspot.com
/2007/02/list-comprehensions-across-languages_18.html)
Retrieved from "http://en.wikipedia.org/wiki/List_comprehension"
Categories: Programming constructs | Articles with example code | Articles with
example Haskell code | Articles with example Python code
Privacy policy
About Wikipedia
Disclaimers