JEFHC@CUNYVM (10/13/90)
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) I've tried every condition under the sun, and have managed to get rid of the .'s (by changin (atom l)(list l) ), but not the NILS. Thanks for your help. .... Julie
raja@copper.ucs.indiana.edu (Raja Sooriamurthi) (10/13/90)
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
eliot@phoenix.Princeton.EDU (Eliot Handelman) (10/13/90)
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)))
eliot@phoenix.Princeton.EDU (Eliot Handelman) (10/13/90)
In article <raja.655772214@copper> raja@copper.ucs.indiana.edu (Raja Sooriamurthi) writes:
;(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]
You probably meant:
(defun mirror (l)
(cond ((null l) nil)
((listp l) (snoc (mirror (rest l)) (mirror (first l))))
(t l)))
yonezawa@cs.uiuc.edu (Noritake Yonezawa) (10/13/90)
In article <3315@idunno.Princeton.EDU> eliot@phoenix.Princeton.EDU (Eliot Handelman) writes: > 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))) How about this? (defun mirror (x) (if (atom x) x (reverse (mapcar #'mirror x)))) -- Noritake Yonezawa [yonezawa@cs.uiuc.edu] Department of Computer Science University of Illinois at Urbana-Champaign
eliot@phoenix.Princeton.EDU (Eliot Handelman) (10/14/90)
In article <YONEZAWA.90Oct13095846@m.m.cs.uiuc.edu> yonezawa@cs.uiuc.edu (Noritake Yonezawa) writes: ;In article <3315@idunno.Princeton.EDU> eliot@phoenix.Princeton.EDU (Eliot Handelman) writes: ; ;> Try this (long code) ;How about this? ; ;(defun mirror (x) ; (if (atom x) ; x ; (reverse (mapcar #'mirror x)))) ; ;-- ;Noritake Yonezawa [yonezawa@cs.uiuc.edu] Sorry, I keep thinking that one is paid by the paren. --eliot
masinter@parc.xerox.com (Larry Masinter) (10/14/90)
> How about this? > (defun mirror (x) > (if (atom x) > x > (reverse (mapcar #'mirror x)))) nreverse is probably better. -- Larry Masinter (masinter@parc.xerox.com) Xerox Palo Alto Research Center (PARC) 3333 Coyote Hill Road; Palo Alto, CA USA 94304 Fax: (415) 494-4333