Glad that the title got your attention :D
I have this little snippet that's driving me crazy:
(defvar cache '(nil . nil))
(defun init-cache ()
(setq cache '(nil . nil)))
(defun save (value)
(nconc cache (list value)))
It's the minimum reproducible example out of a cache I'm trying to implement. I would expect the save
to add elements to the list, and the init-cache
to reset the cache.
Instead:
(init-cache) ; cache is (nil)
(save "foo") ; cache is (nil "foo"). So far, so good
(init-cache) ; cache should be (nil), and it's all downward from here…
(save "bar") ; (nil "foo" "bar") damn you, "foo"! Why are you here? Why are you doing this to me?!?!
So from my un-educated point of view, it looks like setq
isn't working! I'm just changing the cdr of cache with nconc
, why can't I change the cache
symbol's value???
So my questions are:
- what's happening?
- the code looks awkward as hell: is there a more idiomatic way (one that works, if possible =) )
Thank you!