So I'm trying to make a function takes in a list and reverses in place it but I'm not sure how I would use the RPLACA/RPLACD/NONC. Basically does the same thing as reverse but it uses the cons nodes of the original list and does not allocate any new cons nodes. What I have so far is
(defun rip(lst)
(cond (( null lst) 0)
((eq (nil) (cdr (last lst))) 1)
(((setq x (car (last lst)))
(rplaca (car (last lst)) (car first lst))
(rplaca (car first lst) x)) + 2 rip(butlast(rest lst)))))
So a potential list argument would be
(1 2). We could imagine the argument is to the list with address #A and that it looks like this:For each cons we create local variable storing
cdrbefore setting thecdrto the previousconswhich is nil for the firstcons. When the currentconsisnilyour're done and the result is the previouscons. The result for our example would be:The only mutating function you'll need would be
rplacdsince the only thing that changes are thecdr. The function could look something like this:Or if you don't mind leaking you can do this: