Go to the first, previous, next, last section, table of contents.

List Tutorial Exercise 7

Here's one solution. First, compute the triangular list from the previous exercise and type 1 - to subtract one from all the elements.

1:  [ [0],
      [0, 1],
      [0, 1, 2],
      ...

    1 -

The numbers down the lefthand edge of the list we desire are called the "triangular numbers" (now you know why!). The nth triangular number is the sum of the integers from 1 to n, and can be computed directly by the formula @c{$n (n+1) \over 2$} n * (n+1) / 2.

2:  [ [0], [0, 1], ... ]    2:  [ [0], [0, 1], ... ]
1:  [0, 1, 2, 3, 4, 5]      1:  [0, 1, 3, 6, 10, 15]
    .                           .

    v x 6 RET 1 -               V M ' $ ($+1)/2 RET

Adding this list to the above list of lists produces the desired result:

1:  [ [0],
      [1, 2],
      [3, 4, 5],
      [6, 7, 8, 9],
      [10, 11, 12, 13, 14],
      [15, 16, 17, 18, 19, 20] ]
      .

      V M +

If we did not know the formula for triangular numbers, we could have computed them using a V U + command. We could also have gotten them the hard way by mapping a reduction across the original triangular list.

2:  [ [0], [0, 1], ... ]    2:  [ [0], [0, 1], ... ]
1:  [ [0], [0, 1], ... ]    1:  [0, 1, 3, 6, 10, 15]
    .                           .

    RET                         V M V R +

(This means "map a V R + command across the vector," and since each element of the main vector is itself a small vector, V R + computes the sum of its elements.)


Go to the first, previous, next, last section, table of contents.