Page 1 of 1

I have an urgent question :S

Posted: Wed Feb 17, 2010 6:44 am
by koekoek
Dearest members of the Lispforum.
I'm 22 years old and Belgian.

A few months ago, I had an examination about LISP.
We had to write a sorter function.
eg. ´(1 8 4 6 9 7) --> (9 8 7 6 4 1)

This is what I have written:

(defun sorter A
(cond (null A) nil)
(eq (difference ((car a) (greatest A)) 0) (cons (car A) (sorter cdr A)))
(t ( sorter (cons (cdr A) (car A)))))))

defun greatest (A)
and so on..

The teacher said it was totally wrong. But I don't see my mistake? Is it totally wrong? He gave me an F for this :evil:
I will see him tomorrow and I would like to have some professional help. So please tell me, is it totally wrong or are there some good things about what I have written? Would you also give me an F?

Thanks a lot in advance,
kindly regards.

Re: I have an urgent question :S

Posted: Wed Feb 17, 2010 7:08 am
by ramarren
koekoek wrote:A few months ago, I had an examination about LISP.
LISP is not a language. Lisp, spelled with lowercase letters, is a family of languages. You posted into Scheme subforum. Your code is not Scheme. Neither it is valid Common Lisp, or for that matter any kind of Lisp I have ever seen. If you do not even know what language you are supposed to be writing, then I am afraid that you indeed deserve a failing grade. Please determine a correct language so the topic can be moved into an appropriate subforum.

Re: I have an urgent question :S

Posted: Wed Feb 17, 2010 7:12 am
by koekoek
sorry. I forgot to say that I'm not a programmer but an economist. The purpose of our course was that we had to understand the principles of lisp. How it works. Recursion and so on. So it didn't need to be perfect. What I am asking is, is the global idea behind what I have written totally wrong?

My mistake I've put in in the wrong subforum. As I have no administration rights, I can't move the post and I think that double posting won't be appreciated.
Thanks for your help.

Re: I have an urgent question :S

Posted: Wed Feb 17, 2010 7:30 am
by ramarren
One rather important purpose of the program is to be executed. You function does not have a valid syntax according to language grammar, therefore it cannot be compiled, much less executed. When I was studying physics, which is not programming either, having our programs execute was always the very minimum for a passing grade.

I can tell that it is supposed to be a selection sort, but it seems to be made by randomly sticking symbols together. Please think what every line should be doing, and can you explain why it should do that. This requires very minimal programming knowledge and only a few Lisp operators, once you are able to describe the problem as an abstract algorithm.

Re: I have an urgent question :S

Posted: Thu Feb 18, 2010 3:42 pm
by Paul Donnelly
koekoek wrote:

Code: Select all

 ;; Edited the indentation to make some structural issues clearer.
(defun sorter A
  (cond (null A) nil)
  (eq (difference ((car a) (greatest A)) 0) (cons (car A) (sorter cdr A)))
  (t ( sorter (cons (cdr A) (car A))))
;; Where did these guys below come from?
)))
Is it totally wrong?
I'm sorry to say that it is. Your parameter, A, needs to be in parentheses, since defun requires a list of parameters. Your cond form ends far to soon, with only (null A) and nil as its arguments. Your parentheses aren't balanced at all, and in many places are missing or duplicated.

It would be easy to blame Lisp for requiring you to get all those parentheses in place, but please try not to. If you aren't using an editor that will highlight matching parentheses for you, start using one. That will make it easier to balance them right away. Many Lispers use editors that will automatically keep the parentheses balanced, but if you don't plan on writing Lisp again it probably won't be a good use of your time to learn one of these.

Once you get your parens fixed, you need to take another look at the last line. You're not constructing a proper list. Look at this:

Code: Select all

(cons (cons '(1 2 3 ) 4) 7) => (((1 2 3) . 4) . 7)
That's what you've got happening. See what you can do with the append function here.

As far as I can tell, your logic is correct, except for the last line, but the structure of your code is mangled so badly it won't even run. In any language, it's important to make sure the code you've written corresponds to what you actually want it to mean.

Re: I have an urgent question :S

Posted: Fri Feb 19, 2010 1:40 am
by koekoek
Thanks for your reply.
I have to say that we studied lisp without a compiler, so only on paper. Our teacher wanted us to show us how we have to think when programming lisp. I just wanted to hear that my logic is correct thank you :D

Unfortunately, the teacher didn't think so.
No need to reply in this topic anymore.
Greetings to all.