CLisp Programming LISP program that produce a mirrored list

Discussion of Common Lisp

CLisp Programming LISP program that produce a mirrored list

Postby intel1397 » Wed Feb 25, 2009 1:28 pm

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?
2.0
intel1397
 
Posts: 5
Joined: Wed Feb 25, 2009 1:23 pm

Re: CLisp Programming LISP program that produce a mirrored list

Postby fadrian » Wed Feb 25, 2009 2:05 pm

Very carefully.
fadrian
 
Posts: 2
Joined: Mon Dec 29, 2008 3:21 pm

Re: CLisp Programming LISP program that produce a mirrored list

Postby smithzv » Wed Feb 25, 2009 3:20 pm

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
smithzv
 
Posts: 94
Joined: Wed Jul 23, 2008 11:36 am

Re: CLisp Programming LISP program that produce a mirrored list

Postby findinglisp » Wed Feb 25, 2009 4:05 pm

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. :)
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
findinglisp
 
Posts: 440
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX

Re: CLisp Programming LISP program that produce a mirrored list

Postby intel1397 » Wed Feb 25, 2009 5:11 pm

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]


[list=]
((C (D E)) (A B))[/list]

: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
2.0
intel1397
 
Posts: 5
Joined: Wed Feb 25, 2009 1:23 pm

Re: CLisp Programming LISP program that produce a mirrored list

Postby intel1397 » Wed Feb 25, 2009 5:49 pm

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
2.0
intel1397
 
Posts: 5
Joined: Wed Feb 25, 2009 1:23 pm

Re: CLisp Programming LISP program that produce a mirrored list

Postby Harleqin » Wed Feb 25, 2009 9:05 pm

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)))
"Just throw more hardware at it" is the root of all evil.
Svante
Harleqin
 
Posts: 71
Joined: Wed Dec 17, 2008 5:18 am
Location: Bonn, Germany

Re: CLisp Programming LISP program that produce a mirrored list

Postby Paul Donnelly » Wed Feb 25, 2009 11:06 pm

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.
Paul Donnelly
 
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

Re: CLisp Programming LISP program that produce a mirrored list

Postby Harleqin » Thu Feb 26, 2009 6:47 am

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
Harleqin
 
Posts: 71
Joined: Wed Dec 17, 2008 5:18 am
Location: Bonn, Germany

Re: CLisp Programming LISP program that produce a mirrored list

Postby intel1397 » Thu Feb 26, 2009 10:50 am

: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))))
2.0
intel1397
 
Posts: 5
Joined: Wed Feb 25, 2009 1:23 pm

Next

Return to Common Lisp

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests