Next: Putting the function definition together, Previous: Example with incrementing counter, Up: Details of an Incrementing Loop [Contents][Index]

The preceding analysis gives us the bones of our function definition:
first, we will need a variable that we can call `total`

that will
be the total number of pebbles. This will be the value returned by
the function.

Second, we know that the function will require an argument: this
argument will be the total number of rows in the triangle. It can be
called `number-of-rows`

.

Finally, we need a variable to use as a counter. We could call this
variable `counter`

, but a better name is `row-number`

. That
is because what the counter does in this function is count rows, and a
program should be written to be as understandable as possible.

When the Lisp interpreter first starts evaluating the expressions in the
function, the value of `total`

should be set to zero, since we have
not added anything to it. Then the function should add the number of
pebbles in the first row to the total, and then add the number of
pebbles in the second to the total, and then add the number of
pebbles in the third row to the total, and so on, until there are no
more rows left to add.

Both `total`

and `row-number`

are used only inside the
function, so they can be declared as local variables with `let`

and given initial values. Clearly, the initial value for `total`

should be 0. The initial value of `row-number`

should be 1,
since we start with the first row. This means that the `let`

statement will look like this:

(let ((total 0) (row-number 1))body…)

After the internal variables are declared and bound to their initial
values, we can begin the `while`

loop. The expression that serves
as the test should return a value of `t`

for true so long as the
`row-number`

is less than or equal to the `number-of-rows`

.
(If the expression tests true only so long as the row number is less
than the number of rows in the triangle, the last row will never be
added to the total; hence the row number has to be either less than or
equal to the number of rows.)

Lisp provides the `<=`

function that returns true if the value of
its first argument is less than or equal to the value of its second
argument and false otherwise. So the expression that the `while`

will evaluate as its test should look like this:

(<= row-number number-of-rows)

The total number of pebbles can be found by repeatedly adding the number of pebbles in a row to the total already found. Since the number of pebbles in the row is equal to the row number, the total can be found by adding the row number to the total. (Clearly, in a more complex situation, the number of pebbles in the row might be related to the row number in a more complicated way; if this were the case, the row number would be replaced by the appropriate expression.)

(setq total (+ total row-number))

What this does is set the new value of `total`

to be equal to the
sum of adding the number of pebbles in the row to the previous total.

After setting the value of `total`

, the conditions need to be
established for the next repetition of the loop, if there is one. This
is done by incrementing the value of the `row-number`

variable,
which serves as a counter. After the `row-number`

variable has
been incremented, the true-or-false-test at the beginning of the
`while`

loop tests whether its value is still less than or equal to
the value of the `number-of-rows`

and if it is, adds the new value
of the `row-number`

variable to the `total`

of the previous
repetition of the loop.

The built-in Emacs Lisp function `1+`

adds 1 to a number, so the
`row-number`

variable can be incremented with this expression:

(setq row-number (1+ row-number))