Navigation
Synopsis Set values.
Syntax { Exp1, Exp2, ... }
Types
Exp1 Exp2 ... { Exp1, Exp2, ... }
T1 T2 ... set[ lub(T1, T2, ... ) ]
Usage import Set; (included in Prelude)
Description A set is an unordered sequence of values and has the following properties:
  • All elements have the same static type.
  • The order of the elements does not matter.
  • A set contains an element only once. In other words, duplicate elements are eliminated and no matter how many times an element is added to a set, it will occur in it only once.
The type of a set has the form set[T], where T is an arbitrary type.

When a value or variable of type set occurs inside a set, that set value is inserted as set element. To achieve splicing of these elements, i.e., the insertion of the elements of the set value rather than the whole set, it has to be prefixed by the splice operator *.

The following operators are provided on sets:
  • Comprehension: A set comprehension generates a set value.
  • Difference: The difference between two sets.
  • Equal: Equal operator on set values.
  • in: Membership test on set values.
  • Insert: Add an element to a set.
  • Intersection: Intersection of two sets.
  • NotEqual: Not equal operator on set values.
  • notin: Negated membership test on set values.
  • Product: The product of two set values.
  • Splice: Splice the elements of a set in an enclosing set.
  • StrictSubSet: Strict subset operator on set values.
  • StrictSuperSet: Strict superset operator on set values.
  • SubSet: Subset operator on set values.
  • SuperSet: Superset operator on set values.
  • Union: Union of two set values.
The following functions are provided on sets:
  • classify: Classify elements in a set.
  • getOneFrom: Pick a random element from a set.
  • group: Group elements in a set given an equivalence function.
  • index: Map set elements to a fixed index.
  • isEmpty: Test whether a set is empty.
  • itoString: Convert a set to an indented string.
  • mapper: Apply a function to all set elements and return set of results.
  • max: Determine the largest element of a set.
  • min: Smallest element of a set.
  • power: Determine the powerset of a set.
  • power1: The powerset (excluding the empty set) of a set value.
  • reducer: Apply a function to successive elements of a set and combine the results (deprecated).
  • size: Determine the number of elements in a set.
  • sort: Sort the elements of a set. Sort the elements of a set: # Use the built-in ordering on values to compare list elements. # Give an additional lessThan function that will be used to compare elements. This function lessThan (<) function should implement a strict partial order, meaning: # that it is not reflexive, i.e. never a < a # is anti-symmetric, i.e. never a < b && b < a. # is transitive, i.e. if a < b and b < c then a < c.
  • sum: Sum the elements of a set.
  • takeOneFrom: Remove an arbitrary element from a set, returns the element and a set without that element.
  • toList: Convert a set to a list.
  • toMap: Convert a set of tuples to a map; each key is associated with a set of values.
  • toMapUnique: Convert a set of tuples to a map (provided that there are no multiple keys).
  • toString: Convert a set to a string.
  • union: flatten a set of sets into a single set.
Examples

Set types

rascal>{1, 2, 3};
set[int]: {1,2,3}
rascal>{<1,10>, <2,20>, <3,30>};
rel[int,int]: {
  <3,30>,
  <2,20>,
  <1,10>
}
rascal>{1, "b", 3};
set[value]: {1,"b",3}
rascal>{<"a", 10>, <"b", 20>, <"c", 30>}
rel[str,int]: {
  <"c",30>,
  <"b",20>,
  <"a",10>
}
rascal>{{"a", "b"}, {"c", "d", "e"}}
set[set[str]]: {
  {"a","b"},
  {"c","d","e"}
}
Note that
  • {1, 2, 3} and {3, 2, 1} are identical sets (since order is not relevant).
  • {1, 2, 3} and {1, 2, 3, 1} are also identical sets (since duplication is not relevant).

Set splicing

Introduce a set variable S
rascal>S = {1, 2, 3};
set[int]: {1,2,3}
and observe how the value of S is added as single element in another set:
rascal>{10, S, 20};
set[value]: {
  {1,2,3},
  10,
  20
}
or how its elements are added as elements to the other set:
rascal>{10, *S, 20};
set[int]: {1,2,3,10,20}

Questions
Question [1]. Sets can be used to represent a sequence of values when




Question [2]. The type of a set is determined by:




Question [3].
The type of {1, } is set[int]

Question [4].
The type of {"Ginger", "Thyme", "Jar Jar Binks"} is

Question [5].
The type of {-7, "Yoda", 0} is

Question [6]. Fill in the missing operator.
33  {33,34,83} == true

Question [7]. Fill in the missing operator.
81  {69} == {69,81}

Question [8]. Fill in the missing operator.
{65,68,75,77,56,93,30}  {65,68,69,75,15,56,58,91} == {65,68,75,56}

Question [9]. Fill in the missing operator.
{71,8,78,56,27,93}  {0,71,8,56,93,63,46,78} == {27}

Question [10]. Fill in the missing operator.
5  {6, 4, 1} == true

Question [11]. Fill in the missing operator.
{17, 36}  {65,3,36,17,83,20,59} == true

Question [12]. Fill in the missing operator.
{-12, 19}  {6, -12} == {6,19,-12}



Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.