CLisp Programming LISP program that produce a mirrored list
CLisp Programming LISP program that produce a mirrored list
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?
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?
2.0
Re: CLisp Programming LISP program that produce a mirrored list
Very carefully.
Re: CLisp Programming LISP program that produce a mirrored list
The "best" way is usually a poorly defined thing.intel1397 wrote:What is the best way to write a LISP program that will produce a mirror image of a list.
If such a function exists, use it. Typically you would call that function mirror-image, though.intel1397 wrote:Do I use a function such as mirror_image?
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
-
- Posts: 447
- Joined: Sat Jun 28, 2008 7:49 am
- Location: Austin, TX
- Contact:
Re: CLisp Programming LISP program that produce a mirrored list
I like that answer. I'll have to remember it.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.

Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
Re: CLisp Programming LISP program that produce a mirrored list
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
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
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))

(((e d) c) (b a))
Thanks
2.0
Re: CLisp Programming LISP program that produce a mirrored list
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
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
2.0
Re: CLisp Programming LISP program that produce a mirrored list
Touchy, touchy. Why don't you look at the useful part of the answer?
Another hint: What comes out of this?
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: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
Code: Select all
(reverse '((a b) (c (d e))))
Code: Select all
(reverse '(c (d e)))
"Just throw more hardware at it" is the root of all evil.
Svante
Svante
-
- Posts: 148
- Joined: Wed Jul 30, 2008 11:26 pm
Re: CLisp Programming LISP program that produce a mirrored list
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.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.
Re: CLisp Programming LISP program that produce a mirrored list
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
"Just throw more hardware at it" is the root of all evil.
Svante
Svante
Re: CLisp Programming LISP program that produce a mirrored list

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.

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))))
2.0