![]() |
|
Navigation |
Synopsis List in abstract pattern.
Syntax
[ Pat1, Pat2, ..., Patn ]
Description A list pattern matches a list value (the subject), provided that
Pat1 , Pat2 , ..., Patn match the elements of that list in order.
Special cases exist when one of the patterns Pati is
Examples
rascal>import IO;
ok
1. A single variable
rascal>if([10, int N, 30, 40, 50] := [10, 20, 30, 40, 50]) >>>>>>> println("Match succeeded, N = <N>"); Match succeeded, N = 20 ok2. An untyped multi-variable: rascal>if([10, *L, 50] := [10, 20, 30, 40, 50]) >>>>>>> println("Match succeeded, L = <L>"); Match succeeded, L = [20,30,40] ok2. A typed multi-variable: rascal>if([10, *int L, 50] := [10, 20, 30, 40, 50]) >>>>>>> println("Match succeeded, L = <L>"); Match succeeded, L = [20,30,40] okA list pattern may also be non-linear, i.e., it may contain uses of variables that were bound earlier in the pattern: rascal>if([10, *L, 40, L, 50] := [10, 20, 30, 40, 20, 30, 50]) >>>>>>> println("Match succeeded, L = <L>"); Match succeeded, L = [20,30] okHere we see an example, where all pairs of equal elements in a list are printed: rascal>for([*L1, int N, *L2, N, *L3] := [ 5, 10, 20, 30, 40, 30, 15, 20, 10]) >>>>>>> println("N = <N>"); N = 10 N = 20 N = 30 list[void]: []Here we print all ways in which a given list can be partitioned in two lists: rascal>for([*L1, *L2] := [10, 20, 30, 40, 50]) >>>>>>> println("<L1> and <L2>"); [] and [10,20,30,40,50] [10] and [20,30,40,50] [10,20] and [30,40,50] [10,20,30] and [40,50] [10,20,30,40] and [50] [10,20,30,40,50] and [] list[void]: []3. Already declared list variable: rascal>list[int] L; ok rascal>if([10, L, 50] := [10, 20, 30, 40, 50]) >>>>>>> println("Match succeeded, L = <L>"); Match succeeded, L = [20,30,40] ok4. Already declared element variable: rascal>int N; ok rascal>if([10, N, 30, 40, 50] := [10, 20, 30, 40, 50]) >>>>>>> println("Match succeeded, N = <N>"); Match succeeded, N = 20 ok Questions
Question [1].
![]() ![]() ![]() |