Page 1 of 3

CLisp Programming LISP program that produce a mirrored list

Posted: Wed Feb 25, 2009 1:28 pm
by intel1397
What is the best way to write a LISP program that will produce a mirror image of a list.

Example:
(mirror_image '((a b) (c (d e)))) should produce the following (((e d) c) (b a))

Do I use a function such as mirror_image?

Re: CLisp Programming LISP program that produce a mirrored list

Posted: Wed Feb 25, 2009 2:05 pm
by fadrian
Very carefully.

Re: CLisp Programming LISP program that produce a mirrored list

Posted: Wed Feb 25, 2009 3:20 pm
by smithzv
intel1397 wrote:What is the best way to write a LISP program that will produce a mirror image of a list.
The "best" way is usually a poorly defined thing.
intel1397 wrote:Do I use a function such as mirror_image?
If such a function exists, use it. Typically you would call that function mirror-image, though.

This question seems to go under the category of "so easy it must be for homework or learning CL." As such, I'm guessing it is not productive to answer the question.

Perhaps some leading questions:

What have you tried?

This looks a little like reversing the order of the list to me. There is a function REVERSE, but it doesn't do exactly what you want. How is what that does different from what you want?

Zach S

Re: CLisp Programming LISP program that produce a mirrored list

Posted: Wed Feb 25, 2009 4:05 pm
by findinglisp
smithzv wrote:This question seems to go under the category of "so easy it must be for homework or learning CL." As such, I'm guessing it is not productive to answer the question.
I like that answer. I'll have to remember it. :)

Re: CLisp Programming LISP program that produce a mirrored list

Posted: Wed Feb 25, 2009 5:11 pm
by intel1397
Sorry for the delay in response.
The program that im working on can be found below.
If this post is in the wrong section please disregard this issue as of now.

The output of the lisp program is below using reverse

Code: Select all

[list](defun mirror_image (L)
       (if (eq (cdr L) nil)
           L
         (append (mirror_image (cdr L))
               (list (car L)))))[/list]

Code: Select all

 [list](mirror_image '((a b) (c (d e))))[/list]
  • ((C (D E)) (A B))
:arrow: I am now going to try to place whats in reverse in mirror perspective, So that it will look like this.
(((e d) c) (b a))

Thanks

Re: CLisp Programming LISP program that produce a mirrored list

Posted: Wed Feb 25, 2009 5:49 pm
by intel1397
I came to this forum to learn and discuss Lisp Programming of any dialect. Not to have my “easy” question ridiculed and mocked about. Now if there is such a category “so easy it must be for homework or learning CL.” point me in the direction so that I am able to get the help that I need other then some immature juvenile looking to distract a new learner of CLISP. I understand that in order to learn the language one must first put forth at least something regarding the issue in question, and so I will and have done just that.

However if this is the way you all treat each other here then Ill just have to find another forum, for this one has to find valuable solutions to various problems.

findinglisp-Im shocked that you, an Admin would instigate something like that.

Im sorry I came here. I guess I took a wrong turn.
Lets make sure no one else makes the same mistake.

WOT/mcafee site advisor – mediary
GoogleReviews
Intel1397

Re: CLisp Programming LISP program that produce a mirrored list

Posted: Wed Feb 25, 2009 9:05 pm
by Harleqin
Touchy, touchy. Why don't you look at the useful part of the answer?
smithzv wrote: Perhaps some leading questions:

What have you tried?

This looks a little like reversing the order of the list to me. There is a function REVERSE, but it doesn't do exactly what you want. How is what that does different from what you want?

Zach S
The intent is to let you work it out for yourself, giving you some hints. You should look up REVERSE in the Common Lisp HyperSpec, and see what the following produces:

Code: Select all

(reverse '((a b) (c (d e))))
Another hint: What comes out of this?

Code: Select all

(reverse '(c (d e)))

Re: CLisp Programming LISP program that produce a mirrored list

Posted: Wed Feb 25, 2009 11:06 pm
by Paul Donnelly
intel1397 wrote:I came to this forum to learn and discuss Lisp Programming of any dialect. Not to have my “easy” question ridiculed and mocked about. Now if there is such a category “so easy it must be for homework or learning CL.” point me in the direction so that I am able to get the help that I need other then some immature juvenile looking to distract a new learner of CLISP.
Perhaps you shouldn't be so quick to take offense. No one has mocked you here. They just said that, judging by the nature of your question, it would probably be most helpful to you if they pointed you in the right direction and let you work on it yourself.

Re: CLisp Programming LISP program that produce a mirrored list

Posted: Thu Feb 26, 2009 6:47 am
by Harleqin
This is actually a nice exercise, since you need a variety of constructs that are often missing in "blub" languages. Aside from recursion, you need to understand (in alphabetical order)
  • CONSP
  • LAMBDA
  • MAPCAR
  • REVERSE

Re: CLisp Programming LISP program that produce a mirrored list

Posted: Thu Feb 26, 2009 10:50 am
by intel1397
:arrow: Thanks

So in the end looks like Ive found lisp after all or at least the CAR part of it.

So I used your hints and remove cdr from skipping past the first items in the list and used mapcar instead.
Reveresed function is more pronounced.
:?: I not sure as of to how LAMBDA or CONSP would help enhance this function.

Result: (((E D) C) (B A))
Objective: (mirror_image '((a b) (c (d e)))) should produce the following (((e d) c) (b a))

Code: Select all

(defun mirror_image (L)
  (if (atom L)
      L
      (reverse (mapcar 'mirror_image L))))

(mirror_image '((a b) (c (d e))))