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
  1. a VariableDeclaration pattern with a type that is identical to the element type of the subject list: the variable is matched with the value at the corresponding position in the subject list.
  2. a MultiVariable pattern, with an optional element type that is identical to the element type of the subject list: list matching is applied and the variable can match an arbitrary number of elements of the subject list.
  3. a Variable pattern, where the variable has been declared with a list type, but not initialized, outside the pattern: list matching is applied and the variable can match an arbitrary number of elements of the subject list.
  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 the value at the corresponding position in the subject list.
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
ok
2. 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]
ok
2. 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]
ok
A 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]
ok
Here 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]
ok
4. 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]. Complete this function that tests that a list of words consists of two identical sublists:
Fill in
import List;
public bool isReplicated(list[str] words){
  return [*str L, ] := words;
}
and make the following true:
isReplicated(["a", "b", "a", "b"]) == true;



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.