14.8 Recursively Count Words in Different Files

Besides a while loop, you can work on each of a list of files
with recursion. A recursive version of lengths-list-many-files
is short and simple.

The recursive function has the usual parts: the ‘do-again-test’, the
‘next-step-expression’, and the recursive call. The ‘do-again-test’
determines whether the function should call itself again, which it
will do if the list-of-files contains any remaining elements;
the ‘next-step-expression’ resets the list-of-files to the
CDR of itself, so eventually the list will be empty; and the
recursive call calls itself on the shorter list. The complete
function is shorter than this description!

In a sentence, the function returns the lengths’ list for the first of
the list-of-files appended to the result of calling itself on
the rest of the list-of-files.

Here is a test of recursive-lengths-list-many-files, along with
the results of running lengths-list-file on each of the files
individually.

Install recursive-lengths-list-many-files and
lengths-list-file, if necessary, and then evaluate the
following expressions. You may need to change the files’ pathnames;
those here work when this Info file and the Emacs sources are located
in their customary places. To change the expressions, copy them to
the *scratch* buffer, edit them, and then evaluate them.

The results are shown after the ‘⇒’. (These results are
for files from Emacs version 22.1.1; files from other versions of
Emacs may produce different results.)