From: Steve Ellis
Subject: sort function
Date: 
Message-ID: <anUIb.5905$PZ6.2381@news-binary.blueyonder.co.uk>
Hi

can anyone explain how this damn function works, the simpler examples such
as

(sort '(5 2 6 3 1 4) #'>)

seem straight forward enough, the first parameter is the list, the second is
the function used to decide whether or not two elements should be swapped.
But i don't understand the more complicated ones such as

(sort '((9 a) (3 b) (4 c)) #'(lambda (x y) (< (car x) (car y))))

I have no idea what the last part is doing, if it helps what i need is to be
able to sort an association list like in the above example but by the second
element so the list is like this

((a 3) (b 4) (c 5))

but i have no idea what the last part should be to get it to do this

i'd really appreciate any help with this

Steve

From: Johannes Groedem
Subject: Re: sort function
Date: 
Message-ID: <lzk74bvlji.fsf@unity.copyleft.no>
* "Steve Ellis" <··········@blueyonder.co.uk>:

> can anyone explain how this damn function works, the simpler examples such
> as

http://www.lispworks.com/reference/HyperSpec/Body/f_sort_.htm#sort

> But i don't understand the more complicated ones such as
> (sort '((9 a) (3 b) (4 c)) #'(lambda (x y) (< (car x) (car y))))

This is the same as the previous example, except instead of giving a
predefined function (>), you make an anonymous (unnamed) function on
the spot and give it as a parameter to sort.

The function compares the car (first) element of its arguments, so
your unnamed function compares 9 and 3 for the elements (9 a) and (3
b), for example.

Oh, and in my opinion, this is the more appropriate way of doing it:
 (sort '((9 a) (3 b) (4 c)) #'< :key #'car)

See the URL I pointed to for an explanation of the KEY-parameter.

> I have no idea what the last part is doing, if it helps what i need
> is to be able to sort an association list like in the above example
> but by the second element so the list is like this

Doing it the way I showed above it would be simply:
 (sort '((a 3) (b 4) (c 5)) #'< :key #'cadr)

cadr is equivalent to (car (cdr foo)).  (And car/cdr is equivalent to
first/rest.  Look that up, too.)

-- 
Johannes Groedem <OpenPGP: 5055654C>
From: Adam Warner
Subject: Re: sort function
Date: 
Message-ID: <pan.2004.01.01.13.25.02.547106@consulting.net.nz>
Hi Steve Ellis,

> Hi
> 
> can anyone explain how this damn function works, the simpler examples such
> as
> 
> (sort '(5 2 6 3 1 4) #'>)
> 
> seem straight forward enough, the first parameter is the list, the second is
> the function used to decide whether or not two elements should be swapped.
> But i don't understand the more complicated ones such as
> 
> (sort '((9 a) (3 b) (4 c)) #'(lambda (x y) (< (car x) (car y))))
> 
> I have no idea what the last part is doing

It's telling you whether the first element of a list of elements is
numerically less than the first element of another list of elements, e.g.:

(funcall (lambda (x y) (< (car x) (car y))) '(2) '(1)) => nil
(funcall (lambda (x y) (< (car x) (car y))) '(1) '(2)) => t
(funcall (lambda (x y) (< (car x) (car y))) '(2 b) '(1 a)) => nil
(funcall (lambda (x y) (< (car x) (car y))) '(1 a) '(2 b)) => t
(funcall (lambda (x y) (< (car x) (car y))) '(2 it does not matter how many other elements there are) '(1 in the lists)) => nil
(funcall (lambda (x y) (< (car x) (car y))) '(1 it does not matter how many other elements there are) '(2 in the lists)) => t

The anonymous function is a predicate test telling the sort function
whether the elements are in the correct order.

>, if it helps what i need is to be able to sort an association list like
> in the above example but by the second element so the list is like this
> 
> ((a 3) (b 4) (c 5))

So you want to perform a numeric predicate test upon the second elements:
(lambda (x y) (< (second x) (second y)))

Putting it together:
(sort '((a 3) (c 5) (b 4)) (lambda (x y) (< (second x) (second y))))
=> ((a 3) (b 4) (c 5))

> but i have no idea what the last part should be to get it to do this

I suspect you need to brush up on list access and anonymous (lambda)
functions. Plus read and follow the links from SORT in the Hyperspec if
you haven't already.

> i'd really appreciate any help with this

HTH,
Adam
From: Steve Ellis
Subject: Re: sort function
Date: 
Message-ID: <ayfJb.453$0j3.189@news-binary.blueyonder.co.uk>
Hi again

thanks for the help, i understand it all alot better now. The only thing now
thats wrong is that the example Adam gave to sort the list works perfectly
in lispworks on windows but when i try it in GCL under unix i get an error
saying lambda is undefined.
Is this a windows only thing ?? because i'm using the exact same code and it
works fine in windows

thanks again

Steve


"Steve Ellis" <··········@blueyonder.co.uk> wrote in message
························@news-binary.blueyonder.co.uk...
> Hi
>
> can anyone explain how this damn function works, the simpler examples such
> as
>
> (sort '(5 2 6 3 1 4) #'>)
>
> seem straight forward enough, the first parameter is the list, the second
is
> the function used to decide whether or not two elements should be swapped.
> But i don't understand the more complicated ones such as
>
> (sort '((9 a) (3 b) (4 c)) #'(lambda (x y) (< (car x) (car y))))
>
> I have no idea what the last part is doing, if it helps what i need is to
be
> able to sort an association list like in the above example but by the
second
> element so the list is like this
>
> ((a 3) (b 4) (c 5))
>
> but i have no idea what the last part should be to get it to do this
>
> i'd really appreciate any help with this
>
> Steve
>
>
From: Dave Seaman
Subject: Re: sort function
Date: 
Message-ID: <bt410g$avm$1@mozo.cc.purdue.edu>
On Fri, 2 Jan 2004 14:49:31 -0000, Steve Ellis wrote:
> Hi again

> thanks for the help, i understand it all alot better now. The only thing now
> thats wrong is that the example Adam gave to sort the list works perfectly
> in lispworks on windows but when i try it in GCL under unix i get an error
> saying lambda is undefined.

[Gemini:dseaman] $ clisp -q

[1]> (sort '((9 a) (3 b) (4 c)) #'(lambda (x y) (< (car x) (car y))))
((3 B) (4 C) (9 A))
[2]> 


-- 
Dave Seaman
Judge Yohn's mistakes revealed in Mumia Abu-Jamal ruling.
<http://www.commoncouragepress.com/index.cfm?action=book&bookid=228>
From: Thomas A. Russ
Subject: Re: sort function
Date: 
Message-ID: <ymiznd25irw.fsf@sevak.isi.edu>
I seem to be missing part of this thread.  I hope I'm not being too
repetitive here.

Dave Seaman <·······@no.such.host> writes:

> 
> On Fri, 2 Jan 2004 14:49:31 -0000, Steve Ellis wrote:
> > Hi again
> 
> [1]> (sort '((9 a) (3 b) (4 c)) #'(lambda (x y) (< (car x) (car y))))
> ((3 B) (4 C) (9 A))

An easier to understand Common Lisp formulation would be to take
advantage of the :key keyword argument to sort for this sort of
comparison operation:

(sort '((9 a) (3 b) (4 c)) #'< :key #'car)

It seems to more modularly declare the intention of the programmer to
sort using the < predicate applied to the CAR of the data structures.


(For completeness I would also worry about destructively modifying a
constant list argument, but for an example it's not worth sweating).

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Christopher C. Stacy
Subject: Re: sort function
Date: 
Message-ID: <uvfnu9nt3.fsf@news.dtpq.com>
>>>>> On Fri, 2 Jan 2004 14:49:31 -0000, Steve Ellis ("Steve") writes:
 Steve> works perfectly in lispworks on windows but when
 Steve> i try it in GCL under unix i get an error

GCL doesn't implement ANSI Common Lisp, although they appear
to be working towards that goal.  I'd report it as a bug.

<http://www.gnu.org/software/gcl/gcl.html>
From: Fred Gilham
Subject: Re: sort function
Date: 
Message-ID: <u7d69y8cnr.fsf@snapdragon.csl.sri.com>
> thanks for the help, i understand it all alot better now. The only
> thing now thats wrong is that the example Adam gave to sort the list
> works perfectly in lispworks on windows but when i try it in GCL
> under unix i get an error saying lambda is undefined.
> Is this a windows only thing ?? because i'm using the exact same
> code and it works fine in windows

I strongly suspect that in the case where it doesn't work you are
using an old version of GCL.

At first I thought GCL just didn't have the lambda macro and that was
your problem, that is, instead of writing 

(sort '((9 a) (3 b) (4 c)) #'(lambda (x y) (< (car x) (car y))))

you were writing

(sort '((9 a) (3 b) (4 c)) (lambda (x y)  (< (car x) (car y))))


However, both of these work in GCL 2.5.3.

boomerang:~ > gcl
GCL (GNU Common Lisp)  (2.5.3) Mon Jan  5 10:28:09 PST 2004
Licensed under GNU Library General Public License
Dedicated to the memory of W. Schelter

Use (help) to get some basic information on how to use GCL.

>(sort '((9 a) (3 b) (4 c)) #'(lambda (x y) (< (car x) (car y))))

((3 B) (4 C) (9 A))

>(sort '((9 a) (3 b) (4 c)) (lambda (x y)  (< (car x) (car y))))

((3 B) (4 C) (9 A))

Check your GCL version and make sure you are up to date.


-- 
Fred Gilham                                       ······@csl.sri.com
The administration's interest in all e-mail is a wholly unhealthy
precedent, especially given this administration's track record on FBI
files and IRS snooping....                 --- Senator John Ashcroft
From: Steve Ellis
Subject: Re: sort function
Date: 
Message-ID: <_mgJb.224$sq3.48@news-binary.blueyonder.co.uk>
sorry i tried Johannes example and it worked.

thanks to everyone who replied

Steve

"Steve Ellis" <··········@blueyonder.co.uk> wrote in message
························@news-binary.blueyonder.co.uk...
> Hi
>
> can anyone explain how this damn function works, the simpler examples such
> as
>
> (sort '(5 2 6 3 1 4) #'>)
>
> seem straight forward enough, the first parameter is the list, the second
is
> the function used to decide whether or not two elements should be swapped.
> But i don't understand the more complicated ones such as
>
> (sort '((9 a) (3 b) (4 c)) #'(lambda (x y) (< (car x) (car y))))
>
> I have no idea what the last part is doing, if it helps what i need is to
be
> able to sort an association list like in the above example but by the
second
> element so the list is like this
>
> ((a 3) (b 4) (c 5))
>
> but i have no idea what the last part should be to get it to do this
>
> i'd really appreciate any help with this
>
> Steve
>
>