I have an implementation of a function called modify list shown below but it only works for top level lists.
(defun modify-list (old new a-list)
(cond
((null a-list) nil)
((eql (car a-list) old) (cons new (modify-list old new (cdr a-list))))
(T (cons (car a-list)(modify-list old new (cdr a-list))))))
CL-USER 16 : 6 > (modify-list ‘a ‘x ‘(p a d g c a))
(P X D G C X) <– GOOD!
CL-USER 17 : 6 > (modify-list ‘a ‘x ‘(p a d (g a) c a))
(P X D (G A) C X) <—-NOT GOOD!
can anyone help me make this function work on nested lists?
Why not working at an higher level? It would make the code simpler…
Basically instead of assuming
xmust be a list (actually a tree) you just returnnewifxisold, recursively map if it’s a list or otherwise returnxunchanged…With this approach also
(modify 'a 'x 'a) --> X(and that IMO seems right).