www.delorie.com/gnu/docs/emacs-lisp-intro/emacs-lisp-intro_222.html   search  
Buy the book!

Programming in Emacs Lisp

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [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 21.0.100; files from other versions of Emacs may produce different results.)

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

(lengths-list-file "./lisp/macros.el")
     => (273 263 456 90)

(lengths-list-file "./lisp/mail/mailalias.el")
     => (38 32 26 77 174 180 321 198 324)

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

       => (273 263 456 90 38 32 26 77 174 180 321 198 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.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003