// Import the "List" class of the "jazz.util" package
import jazz.util.List;

// Build a list
var l = List.nil.cons(1).cons(2).cons(3).cons(4).cons(5).cons(6);

// Reverse the list
var r = l.reverse();

// Compute the squares
var p = r.map(f) {
  fun f(x) = x * x;
}

// Compute the sum of the elements in the list
var s = p.fold(0, id, (+\2))
{
  fun id(x) = x;
}

// Display the elements of the list "l". The "@" sign is used to force the
// evaluation of the "print" function (this is mandatory in a lazy language)
for (k < l.length()) {
  @ print("l[%d] = %a\n", k, l.nth(k));
}

// Display all the the lists using the default "toString()" method for lists
// but replacing the standard Lisp format by a comma-separated format with
// brackets.
dynamic (List.lparen = "[") {
  dynamic (List.separator = ", ") {
    dynamic (List.rparen = "]") {
      @ print("l = %a\n"
              "r = %a\n"
              "p = %a\n"
              "s = %d\n",
              l, r, p, s);
    }
  }
}