I just stumbled across this phenomenon while trying to implement a simple function that is counting the number of letters in a list.
This is the code:
(defun countit (liste)
(let* ((*dict* '((a 0)(b 0) (c 0) (d 0) (e 0) (f 0) (g 0) (h 0) (i 0)
(j 0) (k 0) (l 0) (m 0) (n 0) (o 0) (p 0) (q 0)(r 0)
(s 0) (t 0) (u 0) (v 0) (w 0) (x 0) (y 0) (z 0))))
(dolist (i liste *dict*)
(incf (second (assoc i *dict*))))))
Interestingly if I run this functon several times the *dict*
keeps the numbers from the last call.
So running (countit '(a a))
and then again (countit '(a a))
yields (a 4)
as a result which I don't quite get why because I'm defining a local environment with let for every function call, right?
Can someone explain to me why this is happening and how I can do it better without changing it too much?