This can be made even simpler. The example searches
me twice. First
assoc searches in
me then
set-ref does search in
me again for the expression found by
assoc.
In this case you could just use
setf:
Code: Select all
(setf (assoc '(80 1010) me) (append $it (list "more stuff")))
me =>
((80 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101))
"more stuff"))
(25 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101)))))
The place reference returned by
assoc can be used by
setf
.. and you can make it even shorter using
push:
Code: Select all
(push "more stuff" (assoc '(80 1010) me) -1)
me =>
((80 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101))
"more stuff"))
(25 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101)))))
push also can use place references and you can better control where to exactly put the new piece:
Code: Select all
(push "more stuff" (assoc '(80 1010) me) -1 -1)
me =>
((80 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101)
"more stuff")))
(25 (1010 ((84 114 117 115 116 80 105 112 101 73 115 65 119 101 115 111 109 101)))))
in this case, the new element is pushed one nesting level higher.