Hello all,
I am writing a function that modifies an association list. In my code I have the following variable:
Thanks for the help.
I am writing a function that modifies an association list. In my code I have the following variable:
(defvar *products* nil)
and the following function:
(defun add-product (alst widget quantity)
(if (assoc widget alst) (setf (cdr (assoc widget alst)) (+ quantity (cdr (assoc widget alst)))) ;if key present update quantity
(setf alst (acons widget quantity alst)))) ;else add (key . data) to alst
When I run this from the terminal I get the following behavior:
>(setf *products* '((hat . 3) (scarf . 4)))
>((hat . 3) (scarf . 4))
>(add-product *products* 'hat 5)
>((hat . 8) (scarf . 4))
>*products*
>((hat . 8) (scarf . 4))
>(add-product *products* 'boots 10)
>((boots . 10) (hat . 8) (scarf . 4))
>*products*
>((hat . 8) (scarf . 3))
I can't seem to figure out why one action will modify the global variable *products* whereas the other will not.Thanks for the help.