all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Incredible shrinking list....
@ 2008-10-17 20:47 Scott
  2008-10-18  4:15 ` Kevin Rodgers
  0 siblings, 1 reply; 3+ messages in thread
From: Scott @ 2008-10-17 20:47 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

I am very much a newbie, so forgive if this is a stupid question.

I am trying to write a function which returns a randomized permutation
of a certain number of items. Here is my stab at it, and the problem
which baffles me:


(defun pick-ten ()
  "Pick a series of ten unique random numbers from 0-9.
You should ensure that random is properly 'seeded', as eg:
  (random t)
prior to using this function."
(let ((all-ten '(0 1 2 3 4 5 6 7 8 9))	;initial list
      (rnd-list '())			;return value
      (nxt 0))				;placeholder for current number
  (while all-ten			;step through the list
    (setq all-ten 			;trim by
     (delq 				;deleting selected number
      (setq nxt 			;but remember number for later
        (car 
          (nthcdr (mod (random)(length all-ten)) all-ten)
        )
     ) all-ten))
   (setq rnd-list (cons nxt rnd-list))	;and put into the return value
  ) 
  rnd-list				;now return it.
))

;; works fine on the first evaluation,
;; but the series somehow shrinks with subsequent calls!
;; what am I doing wrong???!
;;
(pick-ten)(3 2 9 7 1 6 0 5 8 4)
(pick-ten)(1 0 2 3)
(pick-ten)(0 1)
(pick-ten)(0)

(version)"GNU Emacs 21.3.2 (i586-pc-linux-gnu, X toolkit)
 of 2004-07-02 on DrMemory"

(time-to-upgradep (version))t

I'm sure there is a far better way to write this, but I can't for the
life of me figure out why my ham-handed attempt is acting so
bizarrely, and would appreciate any help!


thanks,

Scott swanson





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

* Re: Incredible shrinking list....
  2008-10-17 20:47 Incredible shrinking list Scott
@ 2008-10-18  4:15 ` Kevin Rodgers
  2008-10-21  8:09   ` Alex Schroeder
  0 siblings, 1 reply; 3+ messages in thread
From: Kevin Rodgers @ 2008-10-18  4:15 UTC (permalink / raw)
  To: help-gnu-emacs

Scott wrote:
> Hi,
> 
> I am very much a newbie, so forgive if this is a stupid question.
> 
> I am trying to write a function which returns a randomized permutation
> of a certain number of items. Here is my stab at it, and the problem
> which baffles me:
> 
> 
> (defun pick-ten ()
>   "Pick a series of ten unique random numbers from 0-9.
> You should ensure that random is properly 'seeded', as eg:
>   (random t)
> prior to using this function."
> (let ((all-ten '(0 1 2 3 4 5 6 7 8 9))	;initial list
>       (rnd-list '())			;return value
>       (nxt 0))				;placeholder for current number
>   (while all-ten			;step through the list
>     (setq all-ten 			;trim by
>      (delq 				;deleting selected number
>       (setq nxt 			;but remember number for later
>         (car 
>           (nthcdr (mod (random)(length all-ten)) all-ten)
>         )
>      ) all-ten))
>    (setq rnd-list (cons nxt rnd-list))	;and put into the return value
>   ) 
>   rnd-list				;now return it.
> ))
> 
> ;; works fine on the first evaluation,
> ;; but the series somehow shrinks with subsequent calls!
> ;; what am I doing wrong???!
> ;;
> (pick-ten)(3 2 9 7 1 6 0 5 8 4)
> (pick-ten)(1 0 2 3)
> (pick-ten)(0 1)
> (pick-ten)(0)

Probably destructively modifying a constant -- try this:

(let ((all-ten (list 0 1 2 3 4 5 6 7 8 9))	;initial list

or this:

(setq all-ten (remq ... all-ten))

-- 
Kevin Rodgers
Denver, Colorado, USA





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

* Re: Incredible shrinking list....
  2008-10-18  4:15 ` Kevin Rodgers
@ 2008-10-21  8:09   ` Alex Schroeder
  0 siblings, 0 replies; 3+ messages in thread
From: Alex Schroeder @ 2008-10-21  8:09 UTC (permalink / raw)
  To: help-gnu-emacs

Kevin Rodgers <kevin.d.rodgers <at> gmail.com> writes:

> Probably destructively modifying a constant -- try this:

The wiki has a little more explanation for the original poster...

http://www.emacswiki.org/emacs/DestructiveOperations






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

end of thread, other threads:[~2008-10-21  8:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-10-17 20:47 Incredible shrinking list Scott
2008-10-18  4:15 ` Kevin Rodgers
2008-10-21  8:09   ` Alex Schroeder

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.