Node:lengths-list-many-files, Next:, Previous:Several files, Up:Several files



Determine the lengths of defuns

The design using a while loop is routine. The argument passed the function is a list of files. As we saw earlier (see Loop Example), you can write a while loop so that the body of the loop is evaluated if such a list contains elements, but to exit the loop if the list is empty. For this design to work, the body of the loop must contain an expression that shortens the list each time the body is evaluated, so that eventually the list is empty. The usual technique is to set the value of the list to the value of the CDR of the list each time the body is evaluated.

The template looks like this:

(while test-whether-list-is-empty
  body...
  set-list-to-cdr-of-list)

Also, we remember that a while loop returns nil (the result of evaluating the true-or-false-test), not the result of any evaluation within its body. (The evaluations within the body of the loop are done for their side effects.) However, the expression that sets the lengths' list is part of the body--and that is the value that we want returned by the function as a whole. To do this, we enclose the while loop within a let expression, and arrange that the last element of the let expression contains the value of the lengths' list. (See Loop Example with an Incrementing Counter.)

These considerations lead us directly to the function itself:

;;; Use while loop.
(defun lengths-list-many-files (list-of-files)
  "Return list of lengths of defuns in LIST-OF-FILES."
  (let (lengths-list)

;;; true-or-false-test
    (while list-of-files
      (setq lengths-list
            (append
             lengths-list

;;; Generate a lengths' list.
             (lengths-list-file
              (expand-file-name (car list-of-files)))))

;;; Make files' list shorter.
      (setq list-of-files (cdr list-of-files)))

;;; Return final value of lengths' list.
    lengths-list))

expand-file-name is a built-in function that converts a file name to the absolute, long, path name form of the directory in which the function is called.

Thus, if expand-file-name is called on debug.el when Emacs is visiting the /usr/local/share/emacs/21.0.100/lisp/emacs-lisp/ directory,

debug.el

becomes

/usr/local/share/emacs/21.0.100/lisp/emacs-lisp/debug.el

The only other new element of this function definition is the as yet unstudied function append, which merits a short section for itself.