Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
List comprehension
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Overview== Consider the following example in mathematical [[set-builder notation]]. :<math>S=\{2\cdot x\mid x \in \mathbb{N},\ x^2>3\}</math> or often :<math>S=\{2\cdot x : x \in \mathbb{N},\ x^2>3\}</math> This can be read, "<math>S</math> is the set of all numbers "2 times <math>x</math>" SUCH THAT <math>x</math> is an ELEMENT or MEMBER of the set of [[natural number]]s (<math>\mathbb{N}</math>), AND <math>x</math> squared is greater than <math>3</math>." The smallest natural number, x = 1, fails to satisfy the condition x<sup>2</sup>>3 (the condition 1<sup>2</sup>>3 is false) so 2 Β·1 is not included in S. The next natural number, 2, does satisfy the condition (2<sup>2</sup>>3) as does every other natural number. Thus x consists of 2, 3, 4, 5... Since the set {{var|S}} consists of all numbers "2 times x" it is given by S = {4, 6, 8, 10,...}. S is, in other words, the set of all even numbers greater than 2. In this annotated version of the example: :<math>S=\{\underbrace{2\cdot x}_{\color{Violet}{\text{output expression}}}\mid \underbrace{x}_{\color{Violet}{\text{variable}}} \in \underbrace{\mathbb{N}}_{\color{Violet}{\text{input set}}},\ \underbrace{x^2>3}_{\color{Violet}{\text{predicate}}}\}</math> * <math>x</math> is the variable representing members of an input set. * <math>\mathbb{N}</math> represents the input set, which in this example is the set of natural numbers * <math>x^2>3</math> is a [[Predicate (logic)|predicate]] expression acting as a filter on members of the input set. * <math>2\cdot x</math> is an output expression producing members of the new set from members of the input set that satisfy the predicate expression. * <math>\{\}</math> braces indicate that the result is a set * <math>\mid</math> <math>,</math> the vertical bar is read as "SUCH THAT". The bar and the colon ":" are used interchangeably. * commas separate the predicates and can be read as "AND". A list comprehension has the same syntactic components to represent generation of a list in order from an input [[List (computing)|list]] or [[iterator]]: * A variable representing members of an input list. * An input list (or iterator). * An optional predicate expression. * And an output expression producing members of the output list from members of the input iterable that satisfy the predicate. The order of generation of members of the output list is based on the order of items in the input. In [[Haskell (programming language)|Haskell's]] list comprehension syntax, this set-builder construct would be written similarly, as: <syntaxhighlight lang="haskell"> s = [ 2*x | x <- [0..], x^2 > 3 ] </syntaxhighlight> Here, the list <code>[0..]</code> represents <math>\mathbb{N}</math>, <code>x^2>3</code> represents the predicate, and <code>2*x</code> represents the output expression. List comprehensions give results in a defined order (unlike the members of sets); and list comprehensions may [[Generator (computer science)|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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)