Subject: Re: How to reverse a list...
From: Erik Naggum <>
Date: Tue, 04 Dec 2001 01:40:36 GMT
Newsgroups: comp.lang.lisp
Message-ID: <>

* Steve Long <>
|    (cond
|        ((consp seq-in) ...)
|        ((stringp seq-in) ...)
|        ((vectorp seq-in) ...)
|        (t (error "MY-REVERSE: Arg type ~a not handled."
|                  (type-of seq-in)))))

  This is what the typecase family is for.  Incidentally, (reverse ()) is

(etypecase seq-in
  (null nil)            ;; special case
  (cons ...)
  (string ...)          ;; special case
  (vector ...))

  Note that a string is a vector of type character and thet it might not
  make much of a difference when you have to ensure that you return a
  vector of the same type as you received, just like the standard says.
  This is not particularly hard to do:

(make-array (length sequence) :element-type (array-element-type sequence))

  Since the initial value of the array is irrelevant, you could also use

(copy-seq sequence)

  which does this for you and may be just as fast.

  Traversing a vector is not particularly hard, either:

(do ((forward 0 (1+ forward))
     (backward (1- (length vector)) (1- backward))
     (reversed (copy-seq sequence)))
    ((minusp backward) reversed)
  (setf (aref reversed backward) (aref reversed forward)))

  (There is no chance that any of this would be useful in homework. :)

  The past is not more important than the future, despite what your culture
  has taught you.  Your future observations, conclusions, and beliefs are
  more important to you than those in your past ever will be.  The world is
  changing so fast the balance between the past and the future has shifted.