Syntax for sets in Disco [006U]
- February 19, 2026
Disco gives us a few different ways to write sets.
Set literals in Disco [006V]
- February 19, 2026
We can write literal sets using curly braces and commas in between elements, just as in standard mathematical notation. Notice that the order in which we write the elements, or the number of copies we write of each element, does not matter.
Disco> {1,2,3}
{1, 2, 3}
Disco> {3,1,1,2}
{1, 2, 3}
Disco> {} -- this is how we write the empty set
{}We can also use ellipses to indicate an omitted sequence of elements. Notice that the number of dots in the ellipses, or whether you use commas and/or spaces before and after the ellipses, does not matter.
Disco> {1..5}
{1, 2, 3, 4, 5}
Disco> {1, ......., 5}
{1, 2, 3, 4, 5}
Disco> {1, 3, 5 ... 15}
{1, 3, 5, 7, 9, 11, 13, 15}
Disco> {8 ... 3}
{3, 4, 5, 6, 7, 8}
Disco> {5, 10, 15, ... 40}
{5, 10, 15, 20, 25, 30, 35, 40}
Disco> {1, 4, 9, ... 36}
{1, 4, 9, 16, 25, 36}The last few examples show that Disco can follow basic patterns when using ellipses. For example, {1, 3, 5 ...} will generate odd numbers, {8 ... 3} counts down instead of up, and {5, 10, ...} counts by fives. In the last example, we can even generate square numbers! See the Disco language reference documentation if you are interested to understand exactly what Disco is doing here: basically it is fitting a polynomial to the given points.
Set comprehensions in Disco [006W]
- February 19, 2026
Disco also supports set comprehensions, i.e. set builder notation. For example:
Disco> {x | x in {1..5}} -- same as {1..5}
{1, 2, 3, 4, 5}
Disco> {3x | x in {1..5}} -- multiply each element of {1..5} by 3
{3, 6, 9, 12, 15}
-- Pick out the elements of {1..10} that satisfy the condition
Disco> {x | x in {1 .. 10}, x^2 + 20 == 9x}
{4, 5}
-- Pick out the elements of {1..100} that satisfy all the conditions
Disco> {x | x in {1 .. 100}, x =< 10 \/ x >= 90, x mod 2 == 0}
{2, 4, 6, 8, 10, 90, 92, 94, 96, 98, 100}
-- Products of all combinations of elements from {1..4} and {1, 10, 100}
Disco> {x * y | x in {1 .. 4}, y in {1, 10, 100}}
{1, 2, 3, 4, 10, 20, 30, 40, 100, 200, 300, 400}
-- Pairs of elements from {1..4} where the first is >= the second
Disco> {(x,y) | x in {1 .. 4}, y in {1 .. x}}
{(1, 1), (2, 1), (2, 2), (3, 1), (3, 2), (3, 3), (4, 1), (4, 2), (4, 3), (4, 4)}