Navigation
Synopsis Print a list of squares
Examples How can we print a list of squares? Here is a solution:
module demo::basic::Squares

import IO;                                   

// Print a table of squares

public void squares(int N){
  println("Table of squares from 1 to <N>"); 
  for(int I <- [1 .. N + 1])
      println("<I> squared = <I * I>");      
}

// a solution with a multi line string template:

public str squaresTemplate(int N) 
  = "Table of squares from 1 to <N>
    '<for (int I <- [1 .. N + 1]) {>
    '  <I> squared = <I * I><}>
    ";

At the Rascal:IO module is imported since we want to print things using println.

Rascal:Values/String interpolation is used here several times. In the value of N is inserted in the header message and in the values of I and I * I are inserted in each line that is printed.

Here is how square can be used:
rascal>import demo::basic::Squares;
ok
rascal>squares(9);
Table of squares from 1 to 9
1 squared = 1
2 squared = 4
3 squared = 9
4 squared = 16
5 squared = 25
6 squared = 36
7 squared = 49
8 squared = 64
9 squared = 81
ok
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.