X-Git-Url: http://git.kpe.io/?p=kmrcl.git;a=blobdiff_plain;f=lists.lisp;h=9793f273d863227009390e794295864af9e63536;hp=3115dd5dbe1b99476baf576be214fe4353615686;hb=7446fd15be7c6b489b347d47526eb973f9ed7438;hpb=19190787da0e49afad011b90b4b6ce8608b22444 diff --git a/lists.lisp b/lists.lisp index 3115dd5..9793f27 100644 --- a/lists.lisp +++ b/lists.lisp @@ -151,19 +151,24 @@ (defmacro update-alist (akey value alist &key (test '#'eql) (key '#'identity)) "Macro to support below (setf get-alist)" - (let ((elem (gensym))) - `(let ((,elem (assoc ,akey ,alist :test ,test :key ,key))) - (if ,elem - (progn - (setf (cdr ,elem) ,value) - ,alist) - (setf ,alist (acons ,akey ,value ,alist)))))) + (let ((elem (gensym "ELEM-")) + (val (gensym "VAL-"))) + `(let ((,elem (assoc ,akey ,alist :test ,test :key ,key)) + (,val ,value)) + (cond + (,elem + (setf (cdr ,elem) ,val)) + (,alist + (setf (cdr (last ,alist)) (list (cons ,akey ,val)))) + (t + (setf ,alist (list (cons ,akey ,val))))) + ,alist))) (defun get-alist (key alist &key (test #'eql)) (cdr (assoc key alist :test test))) (defun (setf get-alist) (value key alist &key (test #'eql)) - "This doesn't work to add a field which alist value is only modified locally" + "This won't work if the alist is NIL." (update-alist key value alist :test test) value)