all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* About elisp, Lossing data after sort
@ 2007-03-14  1:51 htbest2000
  2007-03-14  4:39 ` Barry Margolin
  0 siblings, 1 reply; 8+ messages in thread
From: htbest2000 @ 2007-03-14  1:51 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

I am using "GNU Emacs 22.0.94.1 (i386-mingw-nt5.0.2195) of
2007-02-24 on NEUTRINO"

I'm writting a function to generate a series of random numbers,
it seems work fine alone. But the data always lost after sort,
following is the code:


(defun random-list (n)
  "return a list with random numbers from [0,N), length of N"
  (let ((i 0) (list nil))
    (while (< i n)
      (progn (setq list (cons (random n) list))
	     (setq i (+ 1 i))))
    list))

(defun main (n)
  "program entry"
  (let ((list nil))
    (setq list (random-list n))
    (princ (format "%S\n" list))  ;print list
    (sort list '<)
    (princ (format "%S\n" list))  ;print same again
    nil
    ))


For example, running:  (main 10)

the result:
(9 2 8 4 7 8 1 1 9 3)
(9 9)

 What's wrong in my code? Thanks

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

* Re: About elisp, Lossing data after sort
  2007-03-14  1:51 About elisp, Lossing data after sort htbest2000
@ 2007-03-14  4:39 ` Barry Margolin
  2007-03-14  5:24   ` htbest2000
                     ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Barry Margolin @ 2007-03-14  4:39 UTC (permalink / raw)
  To: help-gnu-emacs

In article <1173837100.827152.92620@l75g2000hse.googlegroups.com>,
 "htbest2000" <htbest2000@gmail.com> wrote:

> Hi,
> 
> I am using "GNU Emacs 22.0.94.1 (i386-mingw-nt5.0.2195) of
> 2007-02-24 on NEUTRINO"
> 
> I'm writting a function to generate a series of random numbers,
> it seems work fine alone. But the data always lost after sort,
> following is the code:
> 
> 
> (defun random-list (n)
>   "return a list with random numbers from [0,N), length of N"
>   (let ((i 0) (list nil))
>     (while (< i n)
>       (progn (setq list (cons (random n) list))
> 	     (setq i (+ 1 i))))
>     list))
> 
> (defun main (n)
>   "program entry"
>   (let ((list nil))
>     (setq list (random-list n))
>     (princ (format "%S\n" list))  ;print list
>     (sort list '<)

Change that to:

      (setq list (sort list '<))

>     (princ (format "%S\n" list))  ;print same again
>     nil
>     ))
> 
> 
> For example, running:  (main 10)
> 
> the result:
> (9 2 8 4 7 8 1 1 9 3)
> (9 9)
> 
>  What's wrong in my code? Thanks

-- 
Barry Margolin, barmar@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 ***

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

* Re: About elisp, Lossing data after sort
  2007-03-14  4:39 ` Barry Margolin
@ 2007-03-14  5:24   ` htbest2000
  2007-03-14  8:48     ` Pascal Bourguignon
  2007-03-14  5:25   ` htbest2000
  2007-03-14  5:25   ` htbest2000
  2 siblings, 1 reply; 8+ messages in thread
From: htbest2000 @ 2007-03-14  5:24 UTC (permalink / raw)
  To: help-gnu-emacs

On Mar 14, 12:39 pm, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article <1173837100.827152.92...@l75g2000hse.googlegroups.com>,
>
>
>
>  "htbest2000" <htbest2...@gmail.com> wrote:
> > Hi,
>
> > I am using "GNU Emacs 22.0.94.1 (i386-mingw-nt5.0.2195) of
> > 2007-02-24 on NEUTRINO"
>
> > I'm writting a function to generate a series of random numbers,
> > it seems work fine alone. But the data always lost after sort,
> > following is the code:
>
> > (defun random-list (n)
> >   "return a list with random numbers from [0,N), length of N"
> >   (let ((i 0) (list nil))
> >     (while (< i n)
> >       (progn (setq list (cons (random n) list))
> >         (setq i (+ 1 i))))
> >     list))
>
> > (defun main (n)
> >   "program entry"
> >   (let ((list nil))
> >     (setq list (random-list n))
> >     (princ (format "%S\n" list))  ;print list
> >     (sort list '<)
>
> Change that to:
>
>       (setq list (sort list '<))
>
> >     (princ (format "%S\n" list))  ;print same again
> >     nil
> >     ))
>
> > For example, running:  (main 10)
>
> > the result:
> > (9 2 8 4 7 8 1 1 9 3)
> > (9 9)
>
> >  What's wrong in my code? Thanks
>
> --
> Barry Margolin, bar...@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 ***

Thanks in advance!

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

* Re: About elisp, Lossing data after sort
  2007-03-14  4:39 ` Barry Margolin
  2007-03-14  5:24   ` htbest2000
@ 2007-03-14  5:25   ` htbest2000
  2007-03-14  5:25   ` htbest2000
  2 siblings, 0 replies; 8+ messages in thread
From: htbest2000 @ 2007-03-14  5:25 UTC (permalink / raw)
  To: help-gnu-emacs

On Mar 14, 12:39 pm, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article <1173837100.827152.92...@l75g2000hse.googlegroups.com>,
>
>
>
>  "htbest2000" <htbest2...@gmail.com> wrote:
> > Hi,
>
> > I am using "GNU Emacs 22.0.94.1 (i386-mingw-nt5.0.2195) of
> > 2007-02-24 on NEUTRINO"
>
> > I'm writting a function to generate a series of random numbers,
> > it seems work fine alone. But the data always lost after sort,
> > following is the code:
>
> > (defun random-list (n)
> >   "return a list with random numbers from [0,N), length of N"
> >   (let ((i 0) (list nil))
> >     (while (< i n)
> >       (progn (setq list (cons (random n) list))
> >         (setq i (+ 1 i))))
> >     list))
>
> > (defun main (n)
> >   "program entry"
> >   (let ((list nil))
> >     (setq list (random-list n))
> >     (princ (format "%S\n" list))  ;print list
> >     (sort list '<)
>
> Change that to:
>
>       (setq list (sort list '<))
>
> >     (princ (format "%S\n" list))  ;print same again
> >     nil
> >     ))
>
> > For example, running:  (main 10)
>
> > the result:
> > (9 2 8 4 7 8 1 1 9 3)
> > (9 9)
>
> >  What's wrong in my code? Thanks
>
> --
> Barry Margolin, bar...@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 ***

Thanks in advance!

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

* Re: About elisp, Lossing data after sort
  2007-03-14  4:39 ` Barry Margolin
  2007-03-14  5:24   ` htbest2000
  2007-03-14  5:25   ` htbest2000
@ 2007-03-14  5:25   ` htbest2000
  2 siblings, 0 replies; 8+ messages in thread
From: htbest2000 @ 2007-03-14  5:25 UTC (permalink / raw)
  To: help-gnu-emacs

On Mar 14, 12:39 pm, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article <1173837100.827152.92...@l75g2000hse.googlegroups.com>,
>
>
>
>  "htbest2000" <htbest2...@gmail.com> wrote:
> > Hi,
>
> > I am using "GNU Emacs 22.0.94.1 (i386-mingw-nt5.0.2195) of
> > 2007-02-24 on NEUTRINO"
>
> > I'm writting a function to generate a series of random numbers,
> > it seems work fine alone. But the data always lost after sort,
> > following is the code:
>
> > (defun random-list (n)
> >   "return a list with random numbers from [0,N), length of N"
> >   (let ((i 0) (list nil))
> >     (while (< i n)
> >       (progn (setq list (cons (random n) list))
> >         (setq i (+ 1 i))))
> >     list))
>
> > (defun main (n)
> >   "program entry"
> >   (let ((list nil))
> >     (setq list (random-list n))
> >     (princ (format "%S\n" list))  ;print list
> >     (sort list '<)
>
> Change that to:
>
>       (setq list (sort list '<))
>
> >     (princ (format "%S\n" list))  ;print same again
> >     nil
> >     ))
>
> > For example, running:  (main 10)
>
> > the result:
> > (9 2 8 4 7 8 1 1 9 3)
> > (9 9)
>
> >  What's wrong in my code? Thanks
>
> --
> Barry Margolin, bar...@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 ***

Thanks in advance!

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

* Re: About elisp, Lossing data after sort
  2007-03-14  5:24   ` htbest2000
@ 2007-03-14  8:48     ` Pascal Bourguignon
  2007-03-15 13:07       ` htbest2000
  0 siblings, 1 reply; 8+ messages in thread
From: Pascal Bourguignon @ 2007-03-14  8:48 UTC (permalink / raw)
  To: help-gnu-emacs

"htbest2000" <htbest2000@gmail.com> writes:
> On Mar 14, 12:39 pm, Barry Margolin <bar...@alum.mit.edu> wrote:
>> In article <1173837100.827152.92...@l75g2000hse.googlegroups.com>,
>>  "htbest2000" <htbest2...@gmail.com> wrote:
>> >     (sort list '<)
>>
>> Change that to:
>>
>>       (setq list (sort list '<))
>>
> Thanks in advance!

Too late!


But do you understand why you should use (setq list (sort list '<))
instead of merely (sort list '<)?

-- 
__Pascal Bourguignon__
http://www.informatimago.com
http://pjb.ogamita.org

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

* Re: About elisp, Lossing data after sort
  2007-03-14  8:48     ` Pascal Bourguignon
@ 2007-03-15 13:07       ` htbest2000
  2007-03-15 17:21         ` Joost Kremers
  0 siblings, 1 reply; 8+ messages in thread
From: htbest2000 @ 2007-03-15 13:07 UTC (permalink / raw)
  To: help-gnu-emacs

On Mar 14, 4:48 pm, Pascal Bourguignon <p...@informatimago.com> wrote:
> "htbest2000" <htbest2...@gmail.com> writes:
> > On Mar 14, 12:39 pm, Barry Margolin <bar...@alum.mit.edu> wrote:
> >> In article <1173837100.827152.92...@l75g2000hse.googlegroups.com>,
> >>  "htbest2000" <htbest2...@gmail.com> wrote:
> >> >     (sort list '<)
>
> >> Change that to:
>
> >>       (setq list (sort list '<))
>
> > Thanks in advance!
>
> Too late!
>
> But do you understand why you should use (setq list (sort list '<))
> instead of merely (sort list '<)?
>
> --
> __Pascal Bourguignon__http://www.informatimago.comhttp://pjb.ogamita.org

I'm sorry for my post repeatly. I am using google to post on
internet-bar( low speed ), not tin likes clients.

There something wrong in function `random-list', right? I think
the key is: (setq list (cons (random n) list)). But I don't know
why.

Would you(or others) give an reasonable explanation? Thanks

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

* Re: About elisp, Lossing data after sort
  2007-03-15 13:07       ` htbest2000
@ 2007-03-15 17:21         ` Joost Kremers
  0 siblings, 0 replies; 8+ messages in thread
From: Joost Kremers @ 2007-03-15 17:21 UTC (permalink / raw)
  To: help-gnu-emacs

htbest2000 wrote:
[sort loses data]
> There something wrong in function `random-list', right? I think
> the key is: (setq list (cons (random n) list)). But I don't know
> why.

no, the problem is with the function sort. this is a destructive function,
which means that the data structure that is passed to it as an argument
gets modified. you therefore need to catch the return value of sort and
store it. see the documentation for sort in the elisp manual.

-- 
Joost Kremers                                      joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)

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

end of thread, other threads:[~2007-03-15 17:21 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-03-14  1:51 About elisp, Lossing data after sort htbest2000
2007-03-14  4:39 ` Barry Margolin
2007-03-14  5:24   ` htbest2000
2007-03-14  8:48     ` Pascal Bourguignon
2007-03-15 13:07       ` htbest2000
2007-03-15 17:21         ` Joost Kremers
2007-03-14  5:25   ` htbest2000
2007-03-14  5:25   ` htbest2000

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.