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
?)