Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!udel!princeton!phoenix!eliot From: eliot@phoenix.Princeton.EDU (Eliot Handelman) Newsgroups: comp.lang.lisp Subject: Re: novice question Message-ID: <3315@idunno.Princeton.EDU> Date: 13 Oct 90 04:05:04 GMT References: <90285.160549JEFHC@CUNYVM.BITNET> Sender: news@idunno.Princeton.EDU Organization: Shitson University, New Crapsey Lines: 29 In article <90285.160549JEFHC@CUNYVM.BITNET> JEFHC@CUNYVM writes: ;I realize that this is a very elementary question, but after ;banging my head against the wall for several days I still ;can't find an answer. ; ;I am trying to write a procedure that will produce the ;mirror image of a list, ; (mirror '((a b)(c(d e)))) gives ((( e d)c)(a b) ; but ;(defun mirror (l) ; (cond((null l)nil) ; ((atom l)l) ; (cons (mirror(rest l))(mirror(first l)))))) ; yields ;((NIL (NIL (NIL . E) .D) . C (NIL . B) .A) Try this, which slightly generalizes the trick of consing up the reversed list in an accumulator: (defun mirror (list) (labels ((mirror-1 (list accumulator) (cond ((endp list) accumulator) ((atom (car list)) (mirror-1 (cdr list) (cons (car list) accumulator))) (t (mirror-1 (cdr list) (cons (mirror-1 (car list) nil) accumulator)))))) (mirror-1 list nil)))