Navigation
Synopsis Set in abstract pattern.
Syntax { Pat1, Pat2, ..., Patn }
Description A set pattern matches a set value (the subject), provided that Pat1, Pat2, ..., Patn match the elements of that set in any order (recall that the elements of a set are unordered and do not contain duplicates). Completely analogous to list patterns, there are special cases when one of the patterns Pati is
  1. a VariableDeclaration pattern with a type that is identical to the element type of the subject set: the variable is matched with one value in the subject set.
  2. a MultiVariable pattern, with an optional element type that is identical to the element type of the subject set: set matching is applied and the variable can match an arbitrary number (in arbitrary order) of elements of the subject set.
  3. a Variable pattern, where the variable has been declared with a set type, but not initialized, outside the pattern: set matching is applied and the variable can match an arbitrary number (in arbitrary order) of elements of the subject set.
  4. a Variable pattern, where the variable has been declared with a type equal to the element type of the subject, but not initialized, outside the pattern: the variable is matched with one value in the subject set.
Examples
rascal>import IO;
ok
rascal>
cancelled
1. A single variable
rascal>if({10, 30, 40, 50, int N} := {10, 20, 30, 40, 50})
>>>>>>>   println("Match succeeded, N = <N>");
Match succeeded, N = 20
ok
2. An untyped multi-variable:
rascal>if({10, *S, 50} := {50, 40, 30, 20, 10})
>>>>>>>   println("Match succeeded, S = <S>");
Match succeeded, S = {40,20,30}
ok
2. A typed multi-variable:
rascal>if({10, *int S, 50} := {50, 40, 30, 20, 10})
>>>>>>>   println("Match succeeded, S = <S>");
Match succeeded, S = {40,20,30}
ok
Here we see an example, where all possible splits of a set in two subsets are printed:
rascal>for({*S1, *S2} :={30, 20, 10})
>>>>>>>    println("<S1> and <S2>");
{10,20,30} and {}
{10,20} and {30}
{10,30} and {20}
{10} and {20,30}
{20,30} and {10}
{20} and {10,30}
{30} and {10,20}
{} and {10,20,30}
list[void]: []
3. Already declared set variable:
rascal>set[int] S;
ok
rascal>if({10, *S, 50} := {10, 20, 30, 40, 50})
>>>>>>>   println("Match succeeded, S = <S>");
Match succeeded, S = {40,20,30}
ok
4. Already declared element variable:
rascal>int N;
ok
rascal>if({10, N, 30, 40, 50} := {50, 40, 30, 20, 10})
>>>>>>>   println("Match succeeded, N = <N>");
Match succeeded, N = 20
ok
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.