Contents    Page-10    Prev    Next    Page+10    Index   


Destructive functions in Lisp often begin with n. nreverse reverses a list in place by turning the pointers around.

(nreverse (list 'a 'b 'c))      ->  (c b a)

public static Cons nreverse (Cons lst) {
  Cons last = null; Cons next;
  while (lst != null)
    { next =  rest(lst);
      setrest(lst, last);
      last = lst;
      lst = next; };
  return last; }

(defun nreverse (lst)
  (let (last next)
    (setq last nil)
    (while (not (null lst))
      (setq next (rest lst))
      (setf (rest lst) last)
      (setq last lst)
      (setq lst next) )