First we need a function to construct an empty array. Here is a function **makearray** that recursively creates an x by y array and returns it. It uses a helper function **makelist**:

(defun makearray (x y e) (makelist x (makelist y e))) (defun makelist (n e) (if (zerop n) nil (cons e (makelist (1- n) e))))

The third parameter, **e**, specifies the initial value of each element. Here's an example of its use:

> (setq c (makearray 3 4 0)) ((0 0 0 0) (0 0 0 0) (0 0 0 0))

To access an arbitrary element of a two-dimensional array we use **arrayref**:

(defun arrayref (a x y) (nth y (nth x a)))

Changing an arbitrary element is a bit trickier. Here's one way using a function **changearrayref**, which uses a recursive helper function **changenth**:

(defun changearrayref (a x y n) (changenth a x (changenth (nth x a) y n))) (defun changenth (list x n) (if (null list) nil (cons (if (zerop x) n (car list)) (changenth (cdr list) (1- x) n))))

Here's an example of its use on the array **c** we defined above:

> (changearrayref c 1 2 7) ((0 0 0 0) (0 0 7 0) (0 0 0 0))

Note that this is non-destructive; **c** still has its original value:

> c ((0 0 0 0) (0 0 0 0) (0 0 0 0))

We can write a destructive version of **changearrayref** much more easily, and this was my original attempt at solving this problem:

(defun setarrayref (a x y n) (setf (nth y (nth x a)) n))

I've called it **setarrayref** to distinguish it from the non-destructive version.

Trying this out:

> (setq d (makearray 3 4 0)) ((0 0 0 0) (0 0 0 0) (0 0 0 0)) > (setarrayref d 1 2 7) 7 > d ((0 0 7 0) (0 0 7 0) (0 0 7 0))

Now something's gone seriously wrong – we wanted to change one element but we've ended up changing three. What's the explanation?

The answer is that **makearray** creates a list with shared structure; each occurrence of **(0 0 0 0)** in the result is actually a pointer to the same list, so changing one element of this list appears to change three occurrences in **d**.

One solution would be to use destructive operations, but to disallow creating lists with shared structure; however, shared list structure is one of the aspects of Lisp that makes it highly efficient in list and tree processing tasks.

Therefore the traditional approach to programming in Lisp is to avoid destructive operations, and only use them cautiously only when there's a significant performance benefit in doing so. As I found to my cost in this example, using them can introduce puzzling errors that are hard to track down.

By the way, the modification to **makearray** to ensure that it doesn't create a list with shared structure is:

(defun makearray2 (x y e) (mapcar #'copy-list (makelist x (makelist y e))))

I'd be interested in comments, or suggestions for improvements to the non-destructive version of **changenth**.

For example:

(next '(2 1 3 4 5))

will give:

(2 1 3 5 4)

The permutations are generated in lexical order; in other words, the order they would be listed in a telephone directory, so if we are generating the permutations of the first five digits, the first permutation will be (1 2 3 4 5) and the last one will be (5 4 3 2 1). Calling:

(next '(5 4 3 2 1))

will return **nil** to indicate that there are no more permutations.

The recursive algorithm works as follows:

- We divide the current permutation into the head element, and the remaining elements.

For example, for (2 1 3 4 5) we get 2 and (1 3 4 5).

- Unless the remaining elements are in descending order, the next permutation is the head element followed by the next permutation of the remaining elements.

So, in this case, the next permutation is 2 followed by the next permutation of (1 3 4 5).

- If the remaining elements
*are*in descending order, we need to change the head element to give the next permutation, and follow it with the first permutation of the remaining elements.

For example, for (2 5 4 3 1) calling next on (5 4 3 1) will return **nil**, because there's no next permutation. We therefore need to swap the head element, 2, with the next available digit, and follow it by the first permutation of the remaining digits.

To get the first permutation of the remaining elements we need to arrange them in ascending order. However, we don't need to sort them, because we know they were originally in descending order, so we just need to reverse them: in this example (1 3 4 5).

Now we find the first digit in this list that's greater than the head element. In this case it's 3.

Finally we swap this with the head element to give (3 1 2 4 5) as the next permutation.

To check whether the elements in the list **list** are descending order we can use:

(apply #'> list)

To find the first item in a list **list** that's greater than **item** we use:

(find item list :test #'<)

For example:

> (find 3 '(1 2 4 5 6)) 4

Finally, we use **substitute** to swap the items **old** and **new** in **list**:

(substitute new old list)

For example:

> (substitute 2 3 '(1 3 4 5)) (1 2 4 5)

Finally, here's the definition of **next**, following the recursive definition given above:

(defun next (lst) (cond ((not (apply #'> (cdr lst))) (cons (car lst) (next (cdr lst)))) ((> (car lst) (cadr lst)) nil) (t (let* ((rst (reverse (cdr lst))) (old (find (car lst) rst :test #'<))) (cons old (substitute (car lst) old rst))))))

To test it, here's a routine **all** that repeatedly applies a function to the next permutation of a list until it returns **nil**:

(defun all (function lst) (when lst (funcall function lst) (all function (next lst))))

For example:

> (all print '(1 2 3)) (1 2 3) (1 3 2) (2 1 3) (2 3 1) (3 1 2) (3 2 1) nil]]>

- A simple base case: a terminating scenario that does not use recursion to produce an answer.
- A set of rules that reduce all other cases toward the base case
^{[1]}.

A typical example is the recursive algorithm for factorial:

- factorial(0) is 1, the base case.
- factorial(n) is n * factorial(n-1), reducing all other cases towards the base case.

In Lisp, this is expressed as:

(defun rec1 (n) (cond ((= n 0) 1) (t (* n (rec1 (1- n))))))

But does there always have to be a test for a base case?

How about the following function?

- rec(n) is 1 + (n * rec(n-1)).

This is a recursively-defined function with no explicit base case, and it actually works fine:

- rec(0) is 1.
- rec(1) is 2.
- rec(2) is 5.
- rec(3) is 16.

and so on.

Attempting to write this in Lisp doesn't work, however:

(defun rec (n) (1+ (* n (rec (1- n)))))

Trying it in the Listener:

CL-USER > (rec 1) Stack overflow

The problem is that the ***** operator evaluates all its arguments before invoking the function, causing a stack overflow.

You could argue that when ***** evaluates its first argument, and finds that it's zero, it should just return zero without evaluating any more arguments, in a similar way to **and**.

We can achieve this behaviour by using a macro to define a new version of *, *2:

(defmacro *2 (a b) `(if (zerop ,a) 0 (* ,a ,b)))

Now our function **rec** will work:

(defun rec (n) (1+ (*2 n (rec (1- n)))))

For example:

CL-USER > (rec 10) 9864101

Perhaps it looks as if we've sneaked in a conditional test for a base case through the back door, but that's not really true. An implementation of a Lisp-like language could provide this modified version of the * operator with a slight increase in efficiency, and no other noticeable change.

This is an interesting function; the first 20 terms are as follows:

1, 2, 5, 16, 65, 326, 1957, 13700, 109601, 986410, 9864101, 108505112, 1302061345, 16926797486,

236975164805, 3554627472076, 56874039553217, 966858672404690, 17403456103284421,

330665665962404000

]]>