0

I am learning Emacs lisp and wrote a function which takes as input a list of file name, and returns a list of numbers of each defun in a file (introduced in this tutorial)

(lengths-list-file "/usr/share/emacs/28.0.50/lisp/simpl.el")
=> (49, 49, 49, ... )

I tried to write another function, which takes a list of filenames and return an append-ed list of return values of lengths-list-file of each element in the list.

(defun lengths-list-files2 (list-of-files)
  "Return list of lengths of defuns in LIST-OF-FILES."

  (let (lengths-list)
    (dolist (filename list-of-files lengths-list)
      (let (cur-lengths-list
            (lengths-list-file filename))
        (message "%d" (length cur-lengths-list))
        (setq lengths-list
              (append lengths-list
                      cur-lengths-list))))
    lengths-list))

Here I first used let to bind (lengths-list-file filename) to a symbol cur-length-list, however, I found out that cur-lengths-list was always nil.

Instead, I removed let expression, and I found that it started to work as I expected:

(defun lengths-list-files (list-of-files)
  "Return list of lengths of defuns in LIST-OF-FILES."

  (let (lengths-list)
    (dolist (filename list-of-files lengths-list)
      (setq lengths-list
            (append lengths-list
                    (lengths-list-file filename))))
    lengths-list))

What is the reason that the function with let within the dolist block failed to produce correct result? (i.e., why cur-lengths-list is always nil?)

2
      (let (cur-lengths-list
            (lengths-list-file filename))

Here I first used let to bind (lengths-list-file filename) to a symbol cur-lengths-list

No, you bound cur-lengths-list to nil (hence "I found out that cur-lengths-list was always nil"), and then you bound lengths-list-file to the value of the variable filename.

You wanted this:

      (let ((cur-lengths-list
             (lengths-list-file filename)))

See C-h f let for details.

1
  • Ah, I forgot to wrap it with another pair of parentheses. Thanks – shpark Dec 6 '20 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.