newbie question... 
Author Message
 newbie question...

Quote:

>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.

--

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.



Tue, 21 May 2002 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Newbie Question (Was: Newbie Question...)

2. Not a newbie, but a newbie question...

3. Trivial Newbie Question (Newbie)

4. Newbie Question: Realbasic Question

5. Yet another question on system calls(newbie question)

6. A few more newbie questions..

7. Newbie question about worth of specializing in Smalltalk

8. Very Newbie questions

9. Newbie question

10. newbie questionS

11. Another Newbie Question

12. newbie question

 

 
Powered by phpBB® Forum Software