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
Set-builder notation
(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!
== In programming languages == {{main article|List comprehension}} A similar notation available in a number of [[programming languages]] (notably [[Python (programming language)|Python]] and [[Haskell (programming language)|Haskell]]) is the [[list comprehension]], which combines [[map (higher-order function)|map]] and [[filter (higher-order function)|filter]] operations over one or more [[list (computing)|lists]]. {{section move from|List comprehension|date=December 2023|discuss=Talk:Set-builder notation#Move programming language details and comparison into List comprehension}} In Python, the set-builder's braces are replaced with square brackets, parentheses, or curly braces, giving list, [[generator (computer science)|generator]], and set objects, respectively. Python uses an English-based syntax. Haskell replaces the set-builder's braces with square brackets and uses symbols, including the standard set-builder vertical bar. The same can be achieved in [[Scala (programming language)|Scala]] using Sequence Comprehensions, where the "for" keyword returns a list of the yielded variables using the "yield" keyword.<ref>{{cite web | publisher=Scala | url=http://docs.scala-lang.org/tutorials/tour/sequence-comprehensions.html | title=Sequence Comprehensions | access-date=6 August 2017 }}</ref> Consider these set-builder notation examples in some programming languages: {| class="wikitable" |- ! !! Example 1 !! Example 2 |-style="text-align:center;" ! Set-builder | <math>\{l\ |\ l \in L\}</math> || <math>\{(k, x)\ |\ k \in K \wedge x \in X \wedge P(x) \}</math> |- ! scope="row" | [[Python (programming language)|Python]] | <syntaxhighlight lang="python">{l for l in L}</syntaxhighlight> || <syntaxhighlight lang="python">{(k, x) for k in K for x in X if P(x)}</syntaxhighlight> |- ! scope="row" | [[Haskell (programming language)|Haskell]] | <syntaxhighlight lang="haskell">[l | l <- ls]</syntaxhighlight> || <syntaxhighlight lang="haskell">[(k, x) | k <- ks, x <- xs, p x]</syntaxhighlight> |- ! scope="row" | [[Scala (programming language)|Scala]] | <syntaxhighlight lang="scala">for (l <- L) yield l</syntaxhighlight> || <syntaxhighlight lang="scala">for (k <- K; x <- X if P(x)) yield (k,x)</syntaxhighlight> |- ! scope="row" | [[C Sharp (programming language)|C#]] | <syntaxhighlight lang="csharp">from l in L select l</syntaxhighlight> || <syntaxhighlight lang="csharp">from k in K from x in X where P(x) select (k,x)</syntaxhighlight> |- ! scope="row" | [[SQL]] | <syntaxhighlight lang="sql">SELECT l FROM L_set</syntaxhighlight> || <syntaxhighlight lang="sql"> SELECT k, x FROM K_set, X_set WHERE P(x)</syntaxhighlight> |- ! scope="row" | [[Prolog]] | <syntaxhighlight lang="prolog">setof(L,member(L,Ls),Result)</syntaxhighlight> || <syntaxhighlight lang="prolog">setof((K,X),(member(K,Ks),member(X,Xs),call(P,X)),Result)</syntaxhighlight> |- ![[Erlang (programming language)|Erlang]] |<syntaxhighlight lang="erlang"> [L || L <- Ls] </syntaxhighlight> |<syntaxhighlight lang="erlang"> [{K,X} || K <- Ks, X <- Xs, p(X)] </syntaxhighlight> |- ![[Julia (programming language)|Julia]] |<syntaxhighlight lang="julia">[l for l β L]</syntaxhighlight> |<syntaxhighlight lang="julia"> [(k, x) for k β K for x β X if P(x)] </syntaxhighlight> |- ![[Wolfram Mathematica|Mathematica]] |<syntaxhighlight lang="mathematica"> (l |-> l) /@ L </syntaxhighlight> |<syntaxhighlight lang="mathematica"> Cases[Tuples[{K, X}], {k_, x_} /; P[x]] </syntaxhighlight> |} The set builder notation and list comprehension notation are both instances of a more general notation known as ''monad comprehensions'', which permits map/filter-like operations over any [[monad (functional programming)|monad]] with a [[zero element]].
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)