Let's say have the following list foo
:
(setq foo '(cat dog rat bar))
When I iterate over it using say dolist
, I get an extra nil:
(dolist (ele foo)
(print ele))
cat
dog
rat
bar
nil
Why is there the extra nil
? Shouldn't dolist
only iterate over the list the lenght times? (Over each element)
Of course having seen dotimes
in the documentation along with dolist
I tried to use that as well:
(dotimes (i (length foo))
(print (nth i))))
cat
dog
rat
bar
nil
I also tried (- (length foo) 1)
cat
dog
rat
nil
So here are my questions
- Shouldn't
nil
be omitted in all cases? - Is there a way I can omit that nil (and iterate over all elements)
- I have a feeling that this something to do with linked lists. Can you please explain the missing concept?
Note: I am quite new to elisp and lisp in general. Please be gentle while commenting and answering.
I actually am iterating over an alist
and accessing the keys and values using car
and cdr
.
dolist
sexp interactively, and the command you use to do that first evaluates the sexp and then prints the result it returns. It has nothing to do withdolist
itself or iteration. – Drew Nov 18 '20 at 23:42