Hi,
LISP is basically recursive.
A LISP program is a list of expressions, an expression is either an atom or a list of expressions.
A LISP list (single linked list) is a recursive data structure, a list is either nil (empty) or a cons cell whose tail points either to nil or to a cons cell.
A recursive algorithm to count the number of items of a list should be:
the number of items equals 0 if the list is empty or 1 plus the number of items of the list but first item (cdr).
'the list is empty' is the stop condition
'the number of items of the list but first item' is a recursive call
This can be written in AutoLISP :
Here's the way it works:
(count '(a b c d))
> (+ 1 (count '(b c d)))
>> (+ 1 (+ 1 (count '(c d))))
>>> (+ 1 (+ 1 (+ 1 (count '(d)))))
>>>> (+ 1 (+ 1 (+ 1 (+ 1 (count '())))))
>>>>> (+ 1 (+ 1 (+ 1 (+ 1 0))))
>>>> (+ 1 (+ 1 (+ 1 1)))
>>> (+ 1 (+ 1 2))
>> (+ 1 3)
> 4
Recursion is a natural and elegant way to process lists despite the fact it is most of the time slower and might reach a "stack overflow" if case of a (very) big number of recursive calls.
Recursion is the only way to process recursive data structures as tress (CF LinhPham's folder tree example).