Next: , Previous: , Up: Words in a defun   [Contents][Index]

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!

(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
        (expand-file-name (car list-of-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 22.1.1; files from other versions of Emacs may produce different results.)

(cd "/usr/local/share/emacs/22.1.1/")

(lengths-list-file "./lisp/macros.el")
     ⇒ (283 263 480 90)
(lengths-list-file "./lisp/mail/mailalias.el")
     ⇒ (38 32 29 95 178 180 321 218 324)
(lengths-list-file "./lisp/makesum.el")
     ⇒ (85 181)
       ⇒ (283 263 480 90 38 32 29 95 178 180 321 218 324 85 181)

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.

Next: , Previous: , Up: Words in a defun   [Contents][Index]