tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
65
Task/Pattern-matching/Rascal/pattern-matching-1.rascal
Normal file
65
Task/Pattern-matching/Rascal/pattern-matching-1.rascal
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
// Literal
|
||||
rascal>123 := 123
|
||||
bool: true
|
||||
|
||||
// VariableDeclaration
|
||||
rascal>if(str S := "abc")
|
||||
>>>>>>> println("Match succeeds, S == \"<S>\"");
|
||||
Match succeeds, S == "abc"
|
||||
ok
|
||||
|
||||
// MultiVariable
|
||||
rascal>if([10, N*, 50] := [10, 20, 30, 40, 50])
|
||||
>>>>>>> println("Match succeeds, N == <N>");
|
||||
Match succeeds, N == [20,30,40]
|
||||
ok
|
||||
|
||||
// Variable
|
||||
rascal>N = 10;
|
||||
int: 10
|
||||
rascal>N := 10;
|
||||
bool: true
|
||||
rascal>N := 20;
|
||||
bool: false
|
||||
|
||||
// Set and List
|
||||
rascal>if({10, set[int] S, 50} := {50, 40, 30, 20, 10})
|
||||
>>>>>>> println("Match succeeded, S = <S>");
|
||||
Match succeeded, S = {30,40,20}
|
||||
ok
|
||||
|
||||
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]: []
|
||||
|
||||
// Descendant
|
||||
rascal>T = red(red(black(leaf(1), leaf(2)), black(leaf(3), leaf(4))), black(leaf(5), leaf(4)));
|
||||
rascal>for(/black(_,leaf(4)) := T)
|
||||
>>>>>>> println("Match!");
|
||||
Match!
|
||||
Match!
|
||||
list[void]: []
|
||||
|
||||
rascal>for(/black(_,leaf(int N)) := T)
|
||||
>>>>>>> println("Match <N>");
|
||||
Match 2
|
||||
Match 4
|
||||
Match 4
|
||||
list[void]: []
|
||||
|
||||
rascal>for(/int N := T)
|
||||
>>>>>>> append N;
|
||||
list[int]: [1,2,3,4,5,4]
|
||||
|
||||
// Labelled
|
||||
rascal>for(/M:black(_,leaf(4)) := T)
|
||||
>>>>>>> println("Match <M>");
|
||||
Match black(leaf(3),leaf(4))
|
||||
Match black(leaf(5),leaf(4))
|
||||
list[void]: []
|
||||
Loading…
Add table
Add a link
Reference in a new issue