unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: David Kastrup <dak@gnu.org>
Subject: Re: sorting backwards?
Date: Sun, 15 May 2005 12:17:32 +0200	[thread overview]
Message-ID: <85ll6g3i9v.fsf@lola.goethe.zz> (raw)
In-Reply-To: mailman.6010.1116126541.2819.help-gnu-emacs@gnu.org

Joe Corneli <jcorneli@math.utexas.edu> writes:

> I just posted some code on gnu-emacs-sources that includes some
> sorting.  I want to get numbers in decreasing order, but I found that
> substituting a ">" for a "<" when sorting screws things up.  (Lots of
> contents are omitted.)

No, it isn't.

> Since both < and > are symmetrical mathematically, I don't see why
> this would be.

It isn't.

> I don't have a great example to hand you right now, but if you grab
> the fr3q.el code I just posted and substitute, 
>
>   (let (1graphs)
>     (maphash (lambda (key val) 
>                (setq 1graphs (cons (cons key val) 1graphs)))
>              fr3q-1graphs)
>     (sort 1graphs (lambda (a b) (> (cdr a) (cdr b)))) ...
>
> for
>
>   (let (1graphs)
>     (maphash (lambda (key val) 
>                (setq 1graphs (cons (cons key val) 1graphs)))
>              fr3q-1graphs)
>     (sort 1graphs (lambda (a b) (< (cdr a) (cdr b)))) ...

Both are equally wrong.

C-h f sort RET

    sort is a built-in function in `C source code'.
    (sort LIST PREDICATE)

    Sort LIST, stably, comparing elements using PREDICATE.
    Returns the sorted list.  LIST is modified by side effects.
    PREDICATE is called with two elements of LIST, and should return t
    if the first element is "less" than the second.

RETURNS THE SORTED LIST!!!!!

You throw away the sorted list!  Instead you rely on the side effects
only.  But this can only work in case that the the sorting procedure
does not create _any_ new conses, and that the sorting does _NOT_
change the position of the first element of the list, since this will
_necessarily_ stay the first element of the "sorted list".

In short, it is merely an accident if your code works, and quite
improbable.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

       reply	other threads:[~2005-05-15 10:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.6010.1116126541.2819.help-gnu-emacs@gnu.org>
2005-05-15 10:17 ` David Kastrup [this message]
2005-05-16  6:50   ` sorting backwards? Joe Corneli
     [not found]   ` <mailman.65.1116356089.25862.help-gnu-emacs@gnu.org>
2005-05-17 21:52     ` David Kastrup
2005-05-15 10:56 ` Thien-Thi Nguyen
2005-05-15  3:04 Joe Corneli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=85ll6g3i9v.fsf@lola.goethe.zz \
    --to=dak@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).