lisp trouble

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
I'm trying to sort a list of (d x y) where x and y define a point, and d is a distance from that point to some other point, by the distance.

(sort lis2 '(lambda (p1 p2) (< (car p1) (car p2))))

That produces an invalid predicate error. What am I doing wrong?
 

Extrarius

Senior member
Jul 8, 2001
259
0
0
I'm a Lisp newb so I might be wrong, but I think the problem is you need to make the ' a #'
to make it a 'function pointer'(wrong Lisp term but its the same idea kinda almost).
I'm not entirely sure though and I don't have my reference with me
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Originally posted by: Extrarius
I'm a Lisp newb so I might be wrong, but I think the problem is you need to make the ' a #'
to make it a 'function pointer'(wrong Lisp term but its the same idea kinda almost).
I'm not entirely sure though and I don't have my reference with me

Thanks, that worked. I don't understand why, though... the textbook isn't clear on the difference it makes.
 

Extrarius

Senior member
Jul 8, 2001
259
0
0
The reason is that the predicate argument to sort must be a function (so it can execute some code to do the comparisons it needs). When you use only a single quote, you are giving the function a list. Since it is expecting a function, that causes an error. When you use the pound quote, you are turning the 'stuff quoted' into a list and then marking it as code that can be executed.

I really suggest you buy the book "ANSI Common Lisp" by Paul Graham if you are trying to learn Lisp. It's the best language primer I've read for any language.