Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!sol.ctr.columbia.edu!cica!iuvax!copper!raja From: raja@copper.ucs.indiana.edu (Raja Sooriamurthi) Newsgroups: comp.lang.lisp Subject: Re: novice question Message-ID: Date: 12 Oct 90 22:56:54 GMT References: <90285.160549JEFHC@CUNYVM.BITNET> Sender: news@iuvax.cs.indiana.edu Lines: 34 JEFHC@CUNYVM writes: >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) What you need is a function known as 'snoc' which is similar to cons except that (as its name indicates) it works backwards. So, (snoc '(a b c) 'd) => (a b c d) You could write it as (defun snoc (l x) (append l (list x))) Mirror then becomes, (defun mirror (l) (cond ((null l) nil) ((listp l) (snoc (mirror (rest l)) (mirror (first l)))) (t (snoc (mirror (rest l)) (first l))))) [I might have the syntax mixed up (listp - ?) as I usually program in Scheme, but you get the idea I presume] - Raja raja@cs.indiana.edu