From: Nige Kerkin
Subject: Actions as parameters
Date: 
Message-ID: <74dfdeb7.0204030155.6a81bdd0@posting.google.com>
Hi all, I am a newbie to lisp programming and was just wondering if
anyone could help me with a small problem.

I need a function that will do the following:
(setf a-list '((h 3 k d) (d 12 a p) (x 2 y c)))
	(F_SORT a-list '< 1)       � ((x 2 y c) (h 3 k d) (d 9 a p))
	(F_SORT a-list 'string< 2) � ((d 9 a p) (h 3 k d) (x 2 y c))

And this is what I have so far :

(defun F_SORT (lis action indx)  
  (sort lis #'(lambda(x y)(action (nth indx x) (nth indx y))))
)

But it doesn't what to work!!

Any help would be greatly appreciated!

Thanks

From: Dr. Edmund Weitz
Subject: Re: Actions as parameters
Date: 
Message-ID: <m31ydxulry.fsf@bird.agharta.de>
·······@aol.com (Nige Kerkin) writes:

> Hi all, I am a newbie to lisp programming and was just wondering if
> anyone could help me with a small problem.
> 
> I need a function that will do the following:
> (setf a-list '((h 3 k d) (d 12 a p) (x 2 y c)))
> 	(F_SORT a-list '< 1)       � ((x 2 y c) (h 3 k d) (d 9 a p))
> 	(F_SORT a-list 'string< 2) � ((d 9 a p) (h 3 k d) (x 2 y c))
> 
> And this is what I have so far :
> 
> (defun F_SORT (lis action indx)  
>   (sort lis #'(lambda(x y)(action (nth indx x) (nth indx y))))
> )

Please be honest and tell us if you're asking for help with homework.

What you need is FUNCALL.[1] You should also note that the SORT
function takes a KEY keyword argument which can be used to implement
your function.

Edi.

[1] You might want to look at
    <http://cl-cookbook.sourceforge.net/functions.html>

-- 

Dr. Edmund Weitz
Hamburg
Germany

The Common Lisp Cookbook
<http://cl-cookbook.sourceforge.net/>
From: Nige Kerkin
Subject: Re: Actions as parameters
Date: 
Message-ID: <74dfdeb7.0204031633.1d682506@posting.google.com>
···@agharta.de (Dr. Edmund Weitz) wrote in message news:<··············@bird.agharta.de>...
> ·······@aol.com (Nige Kerkin) writes:
> 
> > Hi all, I am a newbie to lisp programming and was just wondering if
> > anyone could help me with a small problem.
> > 
> > I need a function that will do the following:
> > (setf a-list '((h 3 k d) (d 12 a p) (x 2 y c)))
> > 	(F_SORT a-list '< 1)       � ((x 2 y c) (h 3 k d) (d 9 a p))
> > 	(F_SORT a-list 'string< 2) � ((d 9 a p) (h 3 k d) (x 2 y c))
> > 
> > And this is what I have so far :
> > 
> > (defun F_SORT (lis action indx)  
> >   (sort lis #'(lambda(x y)(action (nth indx x) (nth indx y))))
> > )
> 
> Please be honest and tell us if you're asking for help with homework.
> 
> What you need is FUNCALL.[1] You should also note that the SORT
> function takes a KEY keyword argument which can be used to implement
> your function.
> 
> Edi.
> 
> [1] You might want to look at
>     <http://cl-cookbook.sourceforge.net/functions.html>

Yes it was a homework question but I have spent the last two days
trying to solve the damn thing.

For anyone who is interested the solution to it is...

(defun F_SORT (lis action indx)  
  (sort lis #'(lambda(x y)(funcall action (nth indx x) (nth indx y))))
)

Much appreciation goes to Edi (I don't think I would have worked it
out otherwise) :)

Another question though, I tried to simplify it using the following:

(defun F_SORT (lis action indx)  
 (sort lis action :key '(nth indx lis) )
)

This obviously doesn't work but could someone inform me of whether or
not it is possible to do it this way and if so where can I find out
how.

Thanks Again!
From: Matthieu Villeneuve
Subject: Re: Actions as parameters
Date: 
Message-ID: <3CABA28B.337994D8@tumbleweed.com>
Nige Kerkin wrote:
> 
> ···@agharta.de (Dr. Edmund Weitz) wrote in message news:<··············@bird.agharta.de>...
> > ·······@aol.com (Nige Kerkin) writes:
> >
> > > Hi all, I am a newbie to lisp programming and was just wondering if
> > > anyone could help me with a small problem.
> > >
> > > I need a function that will do the following:
> > > (setf a-list '((h 3 k d) (d 12 a p) (x 2 y c)))
> > >     (F_SORT a-list '< 1)       � ((x 2 y c) (h 3 k d) (d 9 a p))
> > >     (F_SORT a-list 'string< 2) � ((d 9 a p) (h 3 k d) (x 2 y c))
> > >
> > > And this is what I have so far :
> > >
> > > (defun F_SORT (lis action indx)
> > >   (sort lis #'(lambda(x y)(action (nth indx x) (nth indx y))))
> > > )
> >
> > Please be honest and tell us if you're asking for help with homework.
> >
> > What you need is FUNCALL.[1] You should also note that the SORT
> > function takes a KEY keyword argument which can be used to implement
> > your function.
> >
> > Edi.
> >
> > [1] You might want to look at
> >     <http://cl-cookbook.sourceforge.net/functions.html>
> 
> Yes it was a homework question but I have spent the last two days
> trying to solve the damn thing.
> 
> For anyone who is interested the solution to it is...
> 
> (defun F_SORT (lis action indx)
>   (sort lis #'(lambda(x y)(funcall action (nth indx x) (nth indx y))))
> )
> 
> Much appreciation goes to Edi (I don't think I would have worked it
> out otherwise) :)
> 
> Another question though, I tried to simplify it using the following:
> 
> (defun F_SORT (lis action indx)
>  (sort lis action :key '(nth indx lis) )
> )
> 
> This obviously doesn't work but could someone inform me of whether or
> not it is possible to do it this way and if so where can I find out
> how.

The KEY argument to SORT must be either NIL, or a function taking one
argument, the current element of the sequence to be sorted.

  (defun f-sort (list action index)
    (sort list action :key #'(lambda (elt) (nth index elt))))


--Matthieu
From: Tim Moore
Subject: Re: Actions as parameters
Date: 
Message-ID: <a8g8ni$3qk$0@216.39.145.192>
On 3 Apr 2002 16:33:19 -0800, Nige Kerkin <·······@aol.com> wrote:
>···@agharta.de (Dr. Edmund Weitz) wrote in message news:<··············@bird.agharta.de>...
>> ·······@aol.com (Nige Kerkin) writes:
>> 
>> > Hi all, I am a newbie to lisp programming and was just wondering if
>> > anyone could help me with a small problem.
>> > 
>> > I need a function that will do the following:
>> > (setf a-list '((h 3 k d) (d 12 a p) (x 2 y c)))
>> > 	(F_SORT a-list '< 1)       � ((x 2 y c) (h 3 k d) (d 9 a p))
>> > 	(F_SORT a-list 'string< 2) � ((d 9 a p) (h 3 k d) (x 2 y c))
>> > 
...
>Yes it was a homework question but I have spent the last two days
>trying to solve the damn thing.
>
>For anyone who is interested the solution to it is...
>
>(defun F_SORT (lis action indx)  
>  (sort lis #'(lambda(x y)(funcall action (nth indx x) (nth indx y))))
>)
>
>Much appreciation goes to Edi (I don't think I would have worked it
>out otherwise) :)
>
>Another question though, I tried to simplify it using the following:
>
>(defun F_SORT (lis action indx)  
> (sort lis action :key '(nth indx lis) )
>)
>
>This obviously doesn't work but could someone inform me of whether or
>not it is possible to do it this way and if so where can I find out
>how.

I think you're a bit confused about functions, anonymous functions, and lists.

This will do what you want:

(defun f-sort (list action index)
  (sort list action :key #'(lambda (item) (nth index item))))

Note the differences in variable names between my version and yours :)

Tim