Navigation
Synopsis A list comprehension generates a list value.
Syntax [ Exp1, Exp2, ... | Gen1, Gen2, ... ]
Types
Exp1 Exp2 ... [ Exp1, Exp2, ... | Gen1, Gen2, ... ]
T1 T2 ... list[ lub( T1, T2, ... ) ]
Description A list comprehension consists of a number of contributing expressions Exp1, Exp2, ... and a number of generators Gen1, Gen2, Gen3, ... that are evaluated as described in Expressions/Comprehensions.
Examples Computing a list of squares of the numbers from 0 to 10 that are divisible by 3:
rascal>[n * n | int n <- [0 .. 10], n % 3 == 0];
list[int]: [0,9,36,81]
But we can also include the relevant n in the resulting list:
rascal>[n, n * n | int n <- [0 .. 10], n % 3 == 0];
list[int]: [0,0,3,9,6,36,9,81]

Questions
Question [1]. Return the strings that contain a given substring.
Fill in
text = ["An", "honest", "man", "is", "always", "a", "child"]; 
public list[str] find(list[str] text, str contains) = [ s | s <- text,  ];
and make the following true:
find(text, "n") == ["An", "honest", "man"];



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.