list - How to create dotted pair with nil -
i have list of queue names in following form:
'("foo" "bar")
i trying store queues assoc list in following way:
'(("foo" . nil) ("bar" . nil))
basically assoc list queues "foo" , "bar" empty. when bob , alice in "foo" queue should following.
'(("foo" . ("alice" "bob")) ("bar" . nil))
how can create structure? tried achieve writing:
(mapcar #'(lambda (x) (cons x ''nil)) '("foo" "bar"))
which returned
'(("foo" quote nil) ("bar" quote nil))
this not wanted because when tried push bob "foo" queue doesn't work wanted.
* (setf *tmp* '(("foo" . 'nil) ("bar" . 'nil))) (("foo" quote nil) ("bar" quote nil)) * (push "bob" (caddr (assoc "foo" *tmp* :test #'string=))) * *tmp* (("foo" quote ("bob")) ("bar" quote nil))
how can create dotted pair empty list after dot?
edit: when store such assoc list class slot looks fine.
* (describe myclassinstance) ;; ... queues = (("foo" . 'nil) ("bar" . 'nil)) ;; ...
however after writing adding bob "foo" queue queues being changed.
* (push "bob" (caddr (assoc "foo" (slot-value myclassinstance 'testclass::queues) :test #'string=)))) * (describe myclassinstance) ;; ... queues = (("foo" . '("bob") ("bar" . '("bob")) ;; ...
what happened here? looks cdr
part of queues single symbol , when changed it's value in 1 place ("foo" queue) changed in places (all queues). make sense?
a cons cdr nil same one-element list. how lists defined.
in other words, (cons x nil)
same (list x)
. can imagine result this:
+-------+ | x |nil| +-------+
a dotted pair cdr list list.
in other words, '(("foo" . nil) ("bar" . nil))
same '(("foo") ("bar"))
, if former notation perhaps better conveys intent of treating alist.
in same way, '(("foo" . ("alice" "bob")) ("bar" . nil))
same '(("foo" "alice" "bob") ("bar"))
.
this means can create data structure want, can use e. g. list
instead of (lambda (x) (cons x nil))
(which same single argument).
(defun make-queues (&rest names) (mapcar #'list names))
you can push element found assoc
under name:
(defun add-to-queue (queues queue-name item) (push item (cdr (assoc queue-name queues :test #'equal)))) (defun get-queue (queues queue-name) (cdr (assoc queue-name queues :test #'equal)))
the problem had last put literal list , tried modify it: put same literal list containing nil
each element of alist.
Comments
Post a Comment