3 ms·
You should still know about the rplacd function. You can map over it. E.g., delete all middle conses of an assoc list, gluing the first and last one together:
by kazinator 2y ago
You should still know about the rplacd function. You can map over it.
E.g., delete all middle conses of an assoc list, gluing the first and last one together:
[1]> (reduce #'rplacd (list (cons 'a 1)))
(A . 1)
[2]> (reduce #'rplacd (list (cons 'a 1) (cons 'b 2)))
(A B . 2)
[3]> (reduce #'rplacd (list (cons 'a 1) '(cons 'b 2) (cons 'c 3) (cons 'd 4)))
(A D . 4)
If someone finds an application for this, drop me a PM. :)
New people learning Common Lisp don't need to know about rplacd, or not right away. Firstly, destructive manipulation of lists should be deemphasized; it is an advanced topic in itself which can produce bugs.
People who advance to system level work in Lisp, like doing work on implementations, will have to know it. rplacd can appear in code that cannot use setf, because it is processed before setf has been boostrapped.
You will run across rplacd in other people's code even if you don't use it yourself; so if you go beyond just writing programs for yourself into collaboration and maintenance of existing code, you can't avoid knowing about it.
If you ever have to debug the code produced by (setf (cdr place) ...), you will likely see rplaca or a similar function.