From: Joseph
Subject: newbie help with sort
Date: 
Message-ID: <a034c269.0203052218.43442ca0@posting.google.com>
I think I have a pretty trivial test for sort.
I want to sort a list in desending order by the value of
the cdr of the list.  In my case, the values for the cdr
will always be integers.

So I wrote the following function:
(defun desendingByCDR (a b)
  (if (> (cdr a) (cdr b))
      a
      b
  )
)

And I test it with:
(sort '((G . 3) (F . 4) (A . 1)) #'desendingByCDR)

I get back:
((A . 1) (F . 4) (G . 3))


What am I missing?

From: Steven M. Haflich
Subject: Re: newbie help with sort
Date: 
Message-ID: <3C85BE25.7F7C880E@pacbell.net>
Joseph wrote:

> 
> So I wrote the following function:
> (defun desendingByCDR (a b)
>   (if (> (cdr a) (cdr b))
>       a
>       b
>   )
> )
> 
> And I test it with:
> (sort '((G . 3) (F . 4) (A . 1)) #'desendingByCDR)
> 
> I get back:
> ((A . 1) (F . 4) (G . 3))
> 
> What am I missing?

Four things, only one of which is curcial to your question.

1 - The word you wanted is spelled "descending".

2 - Your convention for function naming apparently derives from
Java or whatever.  There is nothing wrong with it, but there is
a convention in Lisp going back probably before you were born
to use hyphen characters rather than capitalization to denote
words.  In ANSI Common Lisp all these characters will be
capitalized and the printer will regurgitate them as
DESENDINGBYCDR which is plainly much harder to read than
DESENDING-BY-CDR.

3 - The sort function is allowed to modify the sequence to be
sorted.  But it is not allowed in CL to modify a literal in
a program (i.e. the quoted list).  Writing code like this can
introduce weird bugs where a function works the first time it
called then fails in successive calls.  You should never pass
something that might be a literal to some function that might
modify it.  You can protect your example this way:

  (sort (copy-list '((g . 3) ...

4 - Finally, here's the answer to your question:  According to
the ANS for Common Lisp, the function argument to sort is a
"a function of two arguments that returns a generalized boolean".
A generalized boolean in Common Lisp is either nil, denoting
false, or anything else, denoting true.  Your function always
returns true (in the form of a real number) which is a silly sort
predicate.
From: Paul Foley
Subject: Re: newbie help with sort
Date: 
Message-ID: <m2vgcarvh9.fsf@mycroft.actrix.gen.nz>
On Wed, 06 Mar 2002 06:58:26 GMT, Steven M Haflich wrote:

> Joseph wrote:

>> 
>> So I wrote the following function:
>> (defun desendingByCDR (a b)
>>   (if (> (cdr a) (cdr b))
>>       a
>>       b
>>   )
>> )
>> 
>> And I test it with:
>> (sort '((G . 3) (F . 4) (A . 1)) #'desendingByCDR)
>> 
>> I get back:
>> ((A . 1) (F . 4) (G . 3))
>> 
>> What am I missing?

[...]
> 4 - Finally, here's the answer to your question:  According to
> the ANS for Common Lisp, the function argument to sort is a
> "a function of two arguments that returns a generalized boolean".
> A generalized boolean in Common Lisp is either nil, denoting
> false, or anything else, denoting true.  Your function always
> returns true (in the form of a real number) which is a silly sort
> predicate.

Actually it always returns a cons, but the point still holds.

The normal way to write this is (SORT your-list #'> :KEY #'CDR)

-- 
Tact is the ability to tell a man he has an open mind when he has a
hole in his head.

(setq reply-to
  (concatenate 'string "Paul Foley " "<mycroft" '(··@) "actrix.gen.nz>"))
From: Joseph
Subject: Re: newbie help with sort
Date: 
Message-ID: <a034c269.0203060525.49a85092@posting.google.com>
Paul Foley <·······@actrix.gen.nz> wrote in message news:<··············@mycroft.actrix.gen.nz>...
> On Wed, 06 Mar 2002 06:58:26 GMT, Steven M Haflich wrote:
> 
> > Joseph wrote:
>  
> >> 
> >> So I wrote the following function:
> >> (defun desendingByCDR (a b)
> >>   (if (> (cdr a) (cdr b))
> >>       a
> >>       b
> >>   )
> >> )
> >> 
> >> And I test it with:
> >> (sort '((G . 3) (F . 4) (A . 1)) #'desendingByCDR)
> >> 
> >> I get back:
> >> ((A . 1) (F . 4) (G . 3))
> >> 
> >> What am I missing?
> 
> [...]
> > 4 - Finally, here's the answer to your question:  According to
> > the ANS for Common Lisp, the function argument to sort is a
> > "a function of two arguments that returns a generalized boolean".
> > A generalized boolean in Common Lisp is either nil, denoting
> > false, or anything else, denoting true.  Your function always
> > returns true (in the form of a real number) which is a silly sort
> > predicate.
> 
> Actually it always returns a cons, but the point still holds.
> 
> The normal way to write this is (SORT your-list #'> :KEY #'CDR)

Thanks everyone,

Obviously I am new to the language and haven't learned the style,
yet.  I actually figured it out after I wrote the post with:
(defun desendingByCDR (a b)
  (> (cdr a) (cdr b))
)
Made me feel kind of dumb for posting, oh well.  I'll have to
look into 'key', though.

Thanks again.
From: Thaddeus L Olczyk
Subject: Re: newbie help with sort
Date: 
Message-ID: <3c913b93.214314109@nntp.interaccess.com>
On 6 Mar 2002 05:25:16 -0800, ···········@yahoo.com (Joseph) wrote:

>Paul Foley <·······@actrix.gen.nz> wrote in message news:<··············@mycroft.actrix.gen.nz>...
>> On Wed, 06 Mar 2002 06:58:26 GMT, Steven M Haflich wrote:
>> 
>> > Joseph wrote:
>>  
>> >> 
>> >> So I wrote the following function:
>> >> (defun desendingByCDR (a b)
>> >>   (if (> (cdr a) (cdr b))
>> >>       a
>> >>       b
>> >>   )
>> >> )
>> >> 
>> >> And I test it with:
>> >> (sort '((G . 3) (F . 4) (A . 1)) #'desendingByCDR)
>> >> 
>> >> I get back:
>> >> ((A . 1) (F . 4) (G . 3))
>> >> 
>> >> What am I missing?
>> 
>> [...]
>> > 4 - Finally, here's the answer to your question:  According to
>> > the ANS for Common Lisp, the function argument to sort is a
>> > "a function of two arguments that returns a generalized boolean".
>> > A generalized boolean in Common Lisp is either nil, denoting
>> > false, or anything else, denoting true.  Your function always
>> > returns true (in the form of a real number) which is a silly sort
>> > predicate.
>> 
>> Actually it always returns a cons, but the point still holds.
>> 
>> The normal way to write this is (SORT your-list #'> :KEY #'CDR)
>
>Thanks everyone,
>
>Obviously I am new to the language and haven't learned the style,
>yet.  I actually figured it out after I wrote the post with:
>(defun desendingByCDR (a b)
>  (> (cdr a) (cdr b))
>)
Shouldn't this be:
(defun desendingByCDR (a b)
  (> (cadr a) (cadr b))
)

or (for newbies)
(defun desendingByCDR (a b)
 (> (car (cdr a)) (car(cdr b)))
)?
From: David Sletten
Subject: Re: newbie help with sort
Date: 
Message-ID: <f57b2559.0203061111.24c15261@posting.google.com>
······@interaccess.com (Thaddeus L Olczyk) wrote in message news:<··················@nntp.interaccess.com>...

> Shouldn't this be:
> (defun desendingByCDR (a b)
>   (> (cadr a) (cadr b))
> )
> 
> or (for newbies)
> (defun desendingByCDR (a b)
>  (> (car (cdr a)) (car(cdr b)))
> )?

The CDR of a dotted (a . b) is b. He is not using proper lists where
CADR (a b) is b.

(Be careful of whom you categorize as newbies...)
From: Thom Goodsell
Subject: Re: newbie help with sort
Date: 
Message-ID: <7v8z95y7vf.fsf@shalott.cra.com>
······@interaccess.com (Thaddeus L Olczyk) writes:
> >> > Joseph wrote:
> >> >> 
> >> >> And I test it with:
> >> >> (sort '((G . 3) (F . 4) (A . 1)) #'desendingByCDR)
                  ^^^     ^^^     ^^^
> >(defun desendingByCDR (a b)
> >  (> (cdr a) (cdr b))
> >)
> Shouldn't this be:
> (defun desendingByCDR (a b)
>   (> (cadr a) (cadr b))
> )

Nope. But if it makes you feel any better, I had the same thought.

Thom
-- 
(map 'string #'(lambda (char) (let ((char-code (char-code char)))
(code-char (if (< 64 char-code 123) (+ (mod (+ 13 char-code) 52) 65)
char-code)))) ····@IXG.IUS")
From: Coby Beck
Subject: Re: newbie help with sort
Date: 
Message-ID: <9Cuj8.129779$kb.7215104@news1.calgary.shaw.ca>
"Joseph" <···········@yahoo.com> wrote in message
·································@posting.google.com...
> I think I have a pretty trivial test for sort.
> I want to sort a list in desending order by the value of
> the cdr of the list.  In my case, the values for the cdr
> will always be integers.
>
> So I wrote the following function:
> (defun desendingByCDR (a b)
>   (if (> (cdr a) (cdr b))
>       a
>       b
>   )
> )
>
> And I test it with:
> (sort '((G . 3) (F . 4) (A . 1)) #'desendingByCDR)
>
> I get back:
> ((A . 1) (F . 4) (G . 3))
>
>
> What am I missing?
(it's like deja vu all over again!)

Should be:
(defun descending-by-cdr (a b)
    (> (cdr a) (cdr b)))

The test should not retrun a or b it should return yes or no.

HTH.

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Barry Margolin
Subject: Re: newbie help with sort
Date: 
Message-ID: <jjvj8.18$on4.425@paloalto-snr2.gtei.net>
In article <·······················@news1.calgary.shaw.ca>,
Coby Beck <·····@mercury.bc.ca> wrote:
>(it's like deja vu all over again!)

You mean the fact that your answer basically reiterates what was written a
week ago?  Did your news server lose the entire rest of the thread?  If so,
you probably shouldn't waste your time answering messages dated so long
ago, as it's likely your answers will be redundant.

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Coby Beck
Subject: Re: newbie help with sort
Date: 
Message-ID: <vbMj8.87832$eb.3732894@news3.calgary.shaw.ca>
"Barry Margolin" <······@genuity.net> wrote in message
·····················@paloalto-snr2.gtei.net...
> In article <·······················@news1.calgary.shaw.ca>,
> Coby Beck <·····@mercury.bc.ca> wrote:
> >(it's like deja vu all over again!)
>
> You mean the fact that your answer basically reiterates what was written a
> week ago?  Did your news server lose the entire rest of the thread?  If
so,
> you probably shouldn't waste your time answering messages dated so long
> ago, as it's likely your answers will be redundant.
>

Well, I didn't look at the date, but remembered seeing the question already
so figured the OP was confused by the other answers and just asked again...I
guess I should have been more careful.

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")