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) (nxt (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

]]>

For example, we want to write a function **pairwise** that can be used as follows:

(pairwise #'- '(1 4 9 16 25 36 49 64 81 100))

This should calculate 4 - 1, 9 - 4, 16 - 9, and so on, and return:

(3 5 7 9 11 13 15 17 19)

Since we need to access more than just one item at a time from the list the obvious starting point is **maplist**. We could try writing something as follows:

(defun pairwise (fun lis) (maplist (lambda (a) (funcall fun (cadr a) (car a))) lis))

However this gives an error because with the above example it's trying to calculate `(- nil 100)` for the last item in the list. We can avoid the error, and then get rid of the **nil** at the end of the result, with:

(defun pairwise (fun lis) (butlast (maplist (lambda (a) (when (cdr a) (funcall fun (cadr a) (car a)))) lis)))

In fact there's a much simpler answer, taking advantage of the fact that the mapping functions can take two (or more) lists as their arguments:

(defun pairwise (fun lis) (map 'list fun (cdr lis) lis))

Furthermore we can easily generalise this to apply a function to any number of successive sets of elements.

Finally here's a practical application of the **pairwise** function. We can represent a polynomial as a list of coefficients; for example, the polynomial:

5x^{4} + 4x^{3} + 3x^{2} + 2x + 1

could be represented as:

(1 2 3 4 5)

where the nth element is the coefficient for x^{n}. The definition of a function **diff** to differentiate a polynomial **poly** is simply:

(defun diff (poly) (pairwise #'* poly))

For example, to differentiate the polynomial given above:

CL-USER > (diff '(1 2 3 4 5)) (2 6 12 20)

So the answer is: 20x^{3} + 12x^{2} + 6x + 2