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
·······@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/>
···@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!
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
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