![]() |
|
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
Examples
rascal>import IO; ok rascal> cancelled1. 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 ok2. 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} ok2. 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} okHere 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} ok4. 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 ![]() |