How to comare this string?

Discussion of Common Lisp
Post Reply
Ceenth
Posts: 4
Joined: Mon Sep 27, 2010 8:55 am

How to comare this string?

Post by Ceenth » Mon Sep 27, 2010 9:27 am

This should maybe be a simple task, but I do have some problem understanding the function and what to insert. Yes, this is an obligatory task I have to deliver by Friday this week, but I can ask anybody about help, except my teacher will of course not tell me the answer. Therefore I’ll try to ask on this LISP Forum.

This is how the task is:

Formulate a LISP function which compares the arrangement of a string pair, where the first element counts most. You can take a starting point with the function my.compare and my.compare2 below. Make sure the function is able to sort a list.

The list: ‘((a b) (b c) (b a) (a a))

And sort it to this list: ‘((a a) (a b) (b a) (b c))

In the function my.compare and my.compare2 you shall compare the arrange on the first element with this function:


(defun my.compare (x y)
(string< (car x) (car y))
)

And then arrange the pair on the second element with this function:


(defun my.compare2 (x y)
(string< (car (cdr x)) (car (cdr y)))
)


So basically the answer shall be like this: ‘((a a) (a b) (b a) (b c))

It is just that I don’t know how to type it in the function, so a little help would be really awesome.

Thanks Ceenth

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: How to comare this string?

Post by ramarren » Mon Sep 27, 2010 10:28 am

You have not shown any attempt to solve the problem on your own, which makes it not clear where your problems are. If you incapable of elementary programming at all, then this is not a correct forum to teach it. It is also not a very good medium to explain Lisp from scratch, for that read a book like Gentle Introduction to Symbolic Computation. In any case this should have been taught before such a task was given.

If you have any specific questions then ask them. You should also know, whether you are supposed to implement a sort yourself, or use a built in sorting operator, which changes the complexity of the answer significantly.

Ceenth
Posts: 4
Joined: Mon Sep 27, 2010 8:55 am

Re: How to comare this string?

Post by Ceenth » Mon Sep 27, 2010 11:27 am

Well, you might have a point. But I'm really trying to solve this. Its just that, when you don't have the basic experience in this type of programming, its kind of hard to figure it out. The reason why I don't have any basic experience, is because I just added this subject to fill my schedule. And now it is to late to change, so I really have to stick to this subject and complete it. I'm actually study Information sience which are totally different from LISP programming. However, I've tried some different functions like:

(sort '((a b)(b c)(b a)(a a))

or:
< (sort '((a b)(b c)(b a)(a a)) # '<)

But everthing seems to bug out, or it even doesn't work. So I feel there are something I should add in the allready functions which I posted allready. But I am not sure.

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: How to comare this string?

Post by gugamilare » Mon Sep 27, 2010 11:41 am

I hope you understand your list is not a list of strings, so you can't use #'STRING< to compare its elements. Strings are delimited by pairs of "quotes". The elements of your list aren't integers either, so you also can't compare them with #'<.

#'< compares integers:

Code: Select all

(sort '(9 8 7 6 5 4 3 2 1) #'<) => (1 2 3 4 5 6 7 8 9)
#'STRING< compares strings:

Code: Select all

(sort '("ab" "bc" "ba" "aa") #'string<) => ("aa" "ab" "ba" "bc")
You will need your own comparison function, your list is a list of lists of symbols. To transform a symbol into a string, you may use the function STRING. Then you need to compare those strings using STRING<.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: How to comare this string?

Post by ramarren » Mon Sep 27, 2010 11:47 am

Lisp programming is not very different from any other kind of programming. The first thing you have to do is analyse the problem independently of the programming language used and formulate the solution in an algorithmic form. This is an useful skill to have, especially in information science domain, even for things which are not strictly speaking programming.

Once you have done that, translating it into a given language is usually fairly simple, although obviously it requires knowing the grammar of the language, in particular the order and meaning of operation arguments.
gugamilare wrote:#'STRING< compares strings:
Actually, STRING< and family compares string designators, which means it can compare symbols by name.

Post Reply