Skip to main content

Set Pattern

rascal-0.40.17

Synopsis

Set in abstract pattern.

Syntax

{Pat₁, Pat₂, * Pat₃, ..., Patₙ}

Description

A set pattern matches a set value (the subject), provided that Pat₁, Pat₂, ..., Patₙ 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 Patᵢ is

  • a MultiVariable, with an optional element type that is an arbitrary sub-type of the element type of the subject set: set matching is applied and the variable can match an arbitrary number of elements of the subject set.

  • a Variable, where the variable has been declared with a subtype of the element type of the subject, but not initialized, outside the pattern: the variable is matched with the value at the corresponding element in the subject set. And the type of the element is checked to match the declared type of the variable.

Examples

rascal>import IO;
ok
  • 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
  • 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
  • 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]: []
  • 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
  • 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