Go to the first, previous, next, last section, table of contents.


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!

(defun recursive-lengths-list-many-files (list-of-files) 
  "Return list of lengths of each defun in LIST-OF-FILES."
  (if list-of-files                     ; do-again-test
      (append
       (lengths-list-file
        (expand-file-name (car list-of-files)))
       (recursive-lengths-list-many-files
        (cdr list-of-files)))))

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 18.57; files from other versions of Emacs may produce different results.)

(lengths-list-file 
 "../lisp/macros.el")
     => (176 154 86)

(lengths-list-file
 "../lisp/mailalias.el")
     => (116 122 265)

(lengths-list-file
 "../lisp/makesum.el")
     => (85 179)

(recursive-lengths-list-many-files
 '("../lisp/macros.el"
   "../lisp/mailalias.el"
   "../lisp/makesum.el"))
       => (176 154 86 116 122 265 85 179)

The recursive-lengths-list-many-files function produces the output we want.

The next step is to prepare the data in the list for display in a graph.


Go to the first, previous, next, last section, table of contents.