nreverse

nreverse (a standard function in Common Lisp) works like reverse, but destructively modifies its input list by turning the pointers around.


> (define lst '(a b c))
lst

> (define back (nreverse lst)) back

> back (c b a)

> lst (a)

nreverse can be implemented as follows:


(define (nreverse x)
  (let ((last '()) (tmp '()))
    (while (pair? x)
      (set! tmp (cdr x))
      (set-cdr! x last)
      (set! last x)
      (set! x tmp))
    last))

Contents    Page-10    Prev    Next    Page+10    Index