0

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)))))
John
  • 1
  • 1
  • 5
    Welcome to stackoverflow. It sounds like you have tried writing some code yourself. You should include that in the question, even if it's incomplete. It gives us a starting point and shows that you made an effort. – genisage Feb 17 '15 at 04:22
  • So far I was not using any of the required functions. (defun reverse-in-place (l) (let ((result l)) (recurse reving ((l l) (r (reverse l)) (cond ((not (consp l)) result) (else (setf (car l) (car r)) (reving (cdr l) (cdr r))))))) – John Feb 17 '15 at 06:56
  • 3
    @John Are you sure you're using Common Lisp? I don't think either `recurse` nor`reving` are things in the language. – zck Feb 17 '15 at 07:15
  • Sorry I added the wrong tag – John Feb 17 '15 at 07:35
  • @zck: RECURSE is a macro used for example in the book Land of Lisp. REVING is then a name introduced in the code snippet here. – Rainer Joswig Feb 17 '15 at 09:05
  • I'm not sure whether you're the same as the anonymous author of the (now deleted) [lisp in place reversing of a list](http://stackoverflow.com/q/28554139/1281433) (the requirements are very similar: in place reversal of a list using RPLACA/RPLACD/NONC). If you are, I *did* miss a bit of your question in one of my comments (sorry!), but this is a *much* better formulation of the question, and includes some attempted code. A good improvement, or a new start if you're new and that wasn't your question! – Joshua Taylor Feb 17 '15 at 15:56
  • I think it was a similar question but I cant see the post. Do you happen to know what it the exact post was? Sorry I'm new to this – John Feb 17 '15 at 18:44
  • @John It was this: http://i.stack.imgur.com/yFJIm.png . It wasn't a good question, but the anonymous user seems to have rage-deleted the question when I (admittedly) missed something in the question. The question content was pretty much identical though (except without code). Is this a homework assignment? It may have been one of your classmates. :) – Joshua Taylor Feb 17 '15 at 21:37
  • @JoshuaTaylor I think it is a fellow classmate. So far I have done the following but I'm not so sure that it is on par. Thoughts? (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))))) – John Feb 18 '15 at 06:29

2 Answers2

0

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:

#A=(1 . #B)
#B=(2 . nil) 

For each cons we create local variable storing cdr before setting the cdr to the previous cons which is nil for the first cons. When the current cons is nil your're done and the result is the previous cons. The result for our example would be:

#A=(1 . nil)
#B=(2 . #A) 

The only mutating function you'll need would be rplacd since the only thing that changes are the cdr. The function could look something like this:

(defun nreverse (list)
  (labels ((aux (list prev)
             (if (endp list)
                 <??>
                 (let ((next <??>))
                   (rplacd <??> <??>)
                   (aux <??> <??>)))))
    (aux list nil)))

Or if you don't mind leaking you can do this:

(defun nreverse (list &optional prev)
  (if (endp list)
      <??>
      (let ((next <??>))
        (rplacd <??> <??>)
        (nreverse <??> <??>))))
Sylwester
  • 47,942
  • 4
  • 47
  • 79
0

So I believe that this was the answer they were looking for:

Recursive: 
(define rip (lst)
(if (null lst) nil (nconc (rip (rest lst))(rplacd lst nil))))

Non-Recursive:
(defun rip (lst)
(do ((res nil) (todo (rest lst)(rest lst)))
    ((null lst) res)
  (setf res (rplacd lst res))
  (setf lst todo) ))
John
  • 1
  • 1