From: Barry Margolin
Subject: Re: newbie question...
Date: 
Message-ID: <AMV14.58$4j4.2250@burlma1-snr2>
In article <·················@inet.com>, doss  <····@unx.dec.com> wrote:
>Hello,
>        I was trying to write a routine that did the following:
>given an input list, reverse the list, and all its atoms and sublists.
>so (A (B C) ) becomes ((C B) A).  I wrote
>the following code:
>
>(DEFUN Rev (X)
>        (IF (NOT (NULL X))
>                (LIST (Rev (CDR X))
>                        (IF (LISTP (CAR X))
>                                (Rev (CAR X))
>                                        (CAR X))))
>)
>
>Which sort of works, but gives the result as ((NIL ((NIL C) B)) A)
>hmmm, can someone lend a helping hand ?
>Regards :-)

You want to join the results of the recursive calls with APPEND or NCONC,
not LIST (NCONC should be safe because you'll be constructing new lists in
the recursion).  And you also need to wrap the result of the recursive work
on (CAR X) into a list before appending them.

I'm not going to provide the actual code; please try to apply the advice
above and implement it yourself, I think you'll learn more.

Another way to implement it would be:

(defun rev (x)
  (if (listp x)
      (nreverse (mapcar #'rev x))
      x))

But the use of NREVERSE might be considered cheating for your homework
problem.  It depends on whether the exercise is in handling the recursion
or in performing the reversing.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.