I am writing a function that modifies an association list. In my code I have the following variable:
Code: Select all
(defvar *products* nil)
Code: Select all
(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
Code: Select all
>(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))
Thanks for the help.