From: Barry Margolin on
In article <hikrkk$m7j$1(a)news.eternal-september.org>,
Joshua Taylor <tayloj(a)cs.rpi.edu> wrote:

> Cecil Westerhof wrote:
> > I want to sort a property list on the property date.
> > I have the function:
> > (defun dates-in-list< (first-list second-list)
> > (let ((first-date (getf first-list :date))
> > (second-date (getf second-list :date)))
> > (string< first-date second-date)))
> >
> > And I sort the list with:
> > (sort *measurements* 'dates-in-lists<)
> >
> > But I do not want to have the list sorted incrementing, but
> > decrementing. Do I understand correctly that I need a function
> > dates-in-lists>? Aka: it is not possible to negate.
>
> Note that the negation, or complement, of dates-in-lists< is not
> dates-in-lists>, but rather dates-in-lists>=. If dates-in-lists>= is OK
> for your purposes, then you can use COMPLEMENT [1]. E.g.,

Unless you're using STABLE-SORT, what difference does it make? It only
affects the equivalent list elements, and SORT doesn't specify how
they're reordered in the first place.

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Cecil Westerhof on
Zach Beane <xach(a)xach.com> writes:

>> I want to sort a property list on the property date.
>> I have the function:
>> (defun dates-in-list< (first-list second-list)
>> (let ((first-date (getf first-list :date))
>> (second-date (getf second-list :date)))
>> (string< first-date second-date)))
>>
>> And I sort the list with:
>> (sort *measurements* 'dates-in-lists<)
>>
>> But I do not want to have the list sorted incrementing, but
>> decrementing. Do I understand correctly that I need a function
>> dates-in-lists>? Aka: it is not possible to negate.
>
> One option might be to use this:
>
> (sort *measurements* (complement 'dates-in-lists<))

Thanks.


> But I think it would be better to use this:
>
> (sort *measurements*
> #'string>
> :key (lambda (object) (getf object :date)))

I agree. No need for functions and when another property needs to be
used, it is very easy to change. And personally I find it clearer also.
Thanks, I'll implement it.


> Be aware that SORT may have side-effects on its first argument.

I know, after the function is finished, the first argument is sorted. By
the way, is this always the case, or is it implementation dependable?

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
From: Zach Beane on
Cecil Westerhof <Cecil(a)decebal.nl> writes:

>> Be aware that SORT may have side-effects on its first argument.
>
> I know, after the function is finished, the first argument is sorted.

I don't think the spec says that in the general case. Here's the part
I think is relevant:

In the case of a list, the list is destructively reordered in the same
manner as for nreverse.

NREVERSE can destroy and re-use its argument and must be used only for
its return value, not its side-effect.

Zach
From: Cecil Westerhof on
Joshua Taylor <tayloj(a)cs.rpi.edu> writes:

>> I want to sort a property list on the property date.
>> I have the function:
>> (defun dates-in-list< (first-list second-list)
>> (let ((first-date (getf first-list :date))
>> (second-date (getf second-list :date)))
>> (string< first-date second-date)))
>>
>> And I sort the list with:
>> (sort *measurements* 'dates-in-lists<)
>>
>> But I do not want to have the list sorted incrementing, but
>> decrementing. Do I understand correctly that I need a function
>> dates-in-lists>? Aka: it is not possible to negate.
>
> Note that the negation, or complement, of dates-in-lists< is not
> dates-in-lists>, but rather dates-in-lists>=. If dates-in-lists>= is OK
> for your purposes, then you can use COMPLEMENT [1]. E.g.,

I know, but in my case the dates are unique, so that is not a problem.
But the solution off Zach Beane I find much more useful as my own, so I
switched.


>> (loop repeat 10 collect (random 100))
> (57 50 29 65 49 46 4 61 9 18)
>
>> (sort * (complement '<))
> (65 61 57 50 49 46 29 18 9 4)

I did not know that with '*' you refer to the last result. Very useful.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
From: Cecil Westerhof on
Zach Beane <xach(a)xach.com> writes:

> Cecil Westerhof <Cecil(a)decebal.nl> writes:
>
>>> Be aware that SORT may have side-effects on its first argument.
>>
>> I know, after the function is finished, the first argument is sorted.
>
> I don't think the spec says that in the general case. Here's the part
> I think is relevant:
>
> In the case of a list, the list is destructively reordered in the same
> manner as for nreverse.
>
> NREVERSE can destroy and re-use its argument and must be used only for
> its return value, not its side-effect.

Okay, I'll always will assign the return value then.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof