all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* sorting backwards?
@ 2005-05-15  3:04 Joe Corneli
  0 siblings, 0 replies; 5+ messages in thread
From: Joe Corneli @ 2005-05-15  3:04 UTC (permalink / raw)



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.)

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

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)))) ...

Maybe you can verify that and/or explain why upon running fr3q-print,
you see something like:

Keys:
/ 1
4 1
2 1
M-v 1
C-/ 1
E 1
M-c 1
( 1
) 1
S 1
A 1
C-  1
M-w 1
C-y 1
insert 1
3 1
C-k 1
> 1
C-M-x 1

instead of (what you expected):

Keys:
o 114
r 84
i 84
u 83
n 80
  74
, 72
a 71
s 59
p 59
e 54
l 49
h 48
g 47
y 43
up 38
down 35
t 33
' 29
c 28
return 25
backspace 20
f 16
d 15
M-f 15
left 11
m 10
; 10
q 9
M-q 9
w 7
v 7
. 6
C-e 6
M-b 6
C-s 6
k 5
M-backspace 5
M-< 5
z 4
b 4
M-> 4
C-left 4
C-l 4
1 3
C-v 3
C-right 3
j 2
x 2
(mouse-1 (#<window 19 on fr3q.el> 561 (398 . 679) 99227077 nil 561 (66 . 13) nil (-64 . 510) (7 . 13))) 2
5 2
C-g 2
I 2
- 2
C-M-x 2
/ 1
4 1
2 1
M-v 1
C-/ 1
E 1
M-c 1
( 1
) 1
S 1
A 1
C-  1
M-w 1
C-y 1
insert 1
3 1
C-k 1
> 1
< 1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: sorting backwards?
       [not found] <mailman.6010.1116126541.2819.help-gnu-emacs@gnu.org>
@ 2005-05-15 10:17 ` David Kastrup
  2005-05-16  6:50   ` Joe Corneli
       [not found]   ` <mailman.65.1116356089.25862.help-gnu-emacs@gnu.org>
  2005-05-15 10:56 ` Thien-Thi Nguyen
  1 sibling, 2 replies; 5+ messages in thread
From: David Kastrup @ 2005-05-15 10:17 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: sorting backwards?
       [not found] <mailman.6010.1116126541.2819.help-gnu-emacs@gnu.org>
  2005-05-15 10:17 ` sorting backwards? David Kastrup
@ 2005-05-15 10:56 ` Thien-Thi Nguyen
  1 sibling, 0 replies; 5+ messages in thread
From: Thien-Thi Nguyen @ 2005-05-15 10:56 UTC (permalink / raw)


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

>     (sort 1graphs (lambda (a b) (< (cdr a) (cdr b)))) ...

do you bind the value returned by `sort'?

thi

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: sorting backwards?
  2005-05-15 10:17 ` sorting backwards? David Kastrup
@ 2005-05-16  6:50   ` Joe Corneli
       [not found]   ` <mailman.65.1116356089.25862.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 5+ messages in thread
From: Joe Corneli @ 2005-05-16  6:50 UTC (permalink / raw)



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

OK (got it!) but FYI it appears to works every time for "<" and never
for ">".  Maybe Emacs is subject to a zeitgeist effect?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: sorting backwards?
       [not found]   ` <mailman.65.1116356089.25862.help-gnu-emacs@gnu.org>
@ 2005-05-17 21:52     ` David Kastrup
  0 siblings, 0 replies; 5+ messages in thread
From: David Kastrup @ 2005-05-17 21:52 UTC (permalink / raw)


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

>    In short, it is merely an accident if your code works, and quite
>    improbable.
>
> OK (got it!) but FYI it appears to works every time for "<" and never
> for ">".  Maybe Emacs is subject to a zeitgeist effect?

If the first element already happens to be the smallest one, things
work out.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-05-17 21:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.6010.1116126541.2819.help-gnu-emacs@gnu.org>
2005-05-15 10:17 ` sorting backwards? David Kastrup
2005-05-16  6:50   ` 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

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.