From: david
Subject: make a list of different random numbers
Date: 
Message-ID: <6d4e7c88-0527-466b-b5d9-0f87cbe1c764@d32g2000yqe.googlegroups.com>
i would like to get a list of 4 different random numbers.
(wk wn wb bk)
i started this code :

(defun random-position () (1+ (random 64)))

(defparameter wk (random-position))

(excl:until (not (= wb wk)) (setf wb (random-position)))

but it is not working. i just need to ensure that none of the
positions
are the same. please to help.

thanks, david

From: ·········@gmail.com
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <608d3b11-14bc-4286-bb74-01e711e67a49@z6g2000pre.googlegroups.com>
On Feb 17, 7:02 am, david <······@gmail.com> wrote:
> i would like to get a list of 4 different random numbers.
> (wk wn wb bk)
> i started this code :
>
> (defun random-position () (1+ (random 64)))
>
> (defparameter wk (random-position))
>
> (excl:until (not (= wb wk)) (setf wb (random-position)))
>
> but it is not working. i just need to ensure that none of the
> positions
> are the same. please to help.
>
> thanks, david

What do you mean by "it is not working"?

Are you expecting the DEFPARAMETER form to define a variable, WK, the
value of which changes each time the variable is subsequently referred
to? If so, that might be the issue... When the DEFPARAMETER form is
evaluated, the (RANDOM-POSITION) form is evaluated as well. Let's say,
for example, (RANDOM-POSITION) gives 17. In that case the DEFPARAMETER
form becomes roughly the equivalent of

(defparameter wk 17)

So... when WK is subsequently referred to, the value 17 will be
returned -- every time...

- Alan
From: Marco Antoniotti
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <e4ac8919-777c-4b2f-b6ef-26247b0cb633@v15g2000yqn.googlegroups.com>
On Feb 17, 4:02 pm, david <······@gmail.com> wrote:
> i would like to get a list of 4 different random numbers.
> (wk wn wb bk)
> i started this code :
>
> (defun random-position () (1+ (random 64)))
>
> (defparameter wk (random-position))
>
> (excl:until (not (= wb wk)) (setf wb (random-position)))
>
> but it is not working. i just need to ensure that none of the
> positions
> are the same. please to help.
>
> thanks, david

Others have already commented on your code.  It is essentially
equivalent to the SLDJ code hereafter.

  def randomPos () return 1 + random(64) end;

  wk = randomPos();

  while (wb == wk) wb = randomPos() end;

So, you should understand while it is not working...

My CL version is

(defun random-pos (&optional (bound 64))
   (loop for rps = (loop repeat 4 collect (random bound))
         until (apply '/= rps)
         finally (return rps)))

Yes.  The above is potentially very wasteful, but I just figured out
that the chance of generating two or more equal numbers gets small for
large BOUND.

There is probably a nice exercise in probabilistic algorithms in the
code snippet above :)  Of course if you replace 4 with a parameter N
and make N close to (or larger than) BOUND....  But then again, you
can pretty safely call RANDOM-POS as

prompt> (random-pos 1.0)

Ah....  the number of positions you can ask for must also be less than
CALL-ARGUMENTS-LIMIT with the code above.

Cheers
--
Marco
From: William James
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <gnf98v01vku@enews2.newsguy.com>
Marco Antoniotti wrote:

> On Feb 17, 4:02�pm, david <······@gmail.com> wrote:
> > i would like to get a list of 4 different random numbers.
> > (wk wn wb bk)
> > i started this code :
> > 
> > (defun random-position () (1+ (random 64)))
> > 
> > (defparameter wk (random-position))
> > 
> > (excl:until (not (= wb wk)) (setf wb (random-position)))
> > 
> > but it is not working. i just need to ensure that none of
> > the positions
> > are the same. please to help.
> > 
> > thanks, david
> 
> Others have already commented on your code.  It is essentially
> equivalent to the SLDJ code hereafter.
> 
>   def randomPos () return 1 + random(64) end;
> 
>   wk = randomPos();
> 
>   while (wb == wk) wb = randomPos() end;
> 
> So, you should understand while it is not working...
> 
> My CL version is
> 
> (defun random-pos (&optional (bound 64))
>    (loop for rps = (loop repeat 4 collect (random bound))
>          until (apply '/= rps)
>          finally (return rps)))
> 
> Yes.  The above is potentially very wasteful, but I just
> figured out that the chance of generating two or more equal
> numbers gets small for large BOUND.
> 
> There is probably a nice exercise in probabilistic algorithms
> in the code snippet above :)  Of course if you replace 4 with
> a parameter N and make N close to (or larger than) BOUND....
> But then again, you can pretty safely call RANDOM-POS as
> 
> prompt> (random-pos 1.0)
> 
> Ah....  the number of positions you can ask for must also be
> less than CALL-ARGUMENTS-LIMIT with the code above.
> 
> Cheers

Ruby:


def random_4 ceiling = 64
  list = []
  ((list << rand(ceiling)).uniq!) until 4 == list.size
  list
end
From: Marco Antoniotti
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <8a48f461-8315-4157-9988-b68e2853492b@v13g2000yqm.googlegroups.com>
On Feb 17, 10:13 pm, "William James" <> wrote:
> Marco Antoniotti wrote:
> > On Feb 17, 4:02 pm, david <······@gmail.com> wrote:
> > > i would like to get a list of 4 different random numbers.
> > > (wk wn wb bk)
> > > i started this code :
>
> > > (defun random-position () (1+ (random 64)))
>
> > > (defparameter wk (random-position))
>
> > > (excl:until (not (= wb wk)) (setf wb (random-position)))
>
> > > but it is not working. i just need to ensure that none of
> > > the positions
> > > are the same. please to help.
>
> > > thanks, david
>
> > Others have already commented on your code.  It is essentially
> > equivalent to the SLDJ code hereafter.
>
> >   def randomPos () return 1 + random(64) end;
>
> >   wk = randomPos();
>
> >   while (wb == wk) wb = randomPos() end;
>
> > So, you should understand while it is not working...
>
> > My CL version is
>
> > (defun random-pos (&optional (bound 64))
> >    (loop for rps = (loop repeat 4 collect (random bound))
> >          until (apply '/= rps)
> >          finally (return rps)))
>
> > Yes.  The above is potentially very wasteful, but I just
> > figured out that the chance of generating two or more equal
> > numbers gets small for large BOUND.
>
> > There is probably a nice exercise in probabilistic algorithms
> > in the code snippet above :)  Of course if you replace 4 with
> > a parameter N and make N close to (or larger than) BOUND....
> > But then again, you can pretty safely call RANDOM-POS as
>
> > prompt> (random-pos 1.0)
>
> > Ah....  the number of positions you can ask for must also be
> > less than CALL-ARGUMENTS-LIMIT with the code above.
>
> > Cheers
>
> Ruby:
>
> def random_4 ceiling = 64
>   list = []
>   ((list << rand(ceiling)).uniq!) until 4 == list.size
>   list
> end

Now.  That's more likely!  Finally some Ruby.  Run it as fast as the
CL version!

I'll grant you that this is even better than my original one.

(defun random-pos (&optional (bound 1.0))
   (loop collect (random bound) into list
         until (= 4 (length (setf list (delete-duplicates list :test
'=))
         finally (return list))

a little more verbose, but same thing.  Plus I can compile it.

Cheers

Cheers
--
Marco
From: Rob Warnock
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <Tt6dnXoBBpIQcgbUnZ2dnUVZ_jmdnZ2d@speakeasy.net>
Marco Antoniotti  <·······@gmail.com> wrote:
+---------------
| "William James" <> wrote:
| > Marco Antoniotti wrote:
| > > On Feb 17, 4:02�pm, david <······@gmail.com> wrote:
| > > > i would like to get a list of 4 different random numbers.
| > > > (wk wn wb bk)
| > > > i started this code :
| > > > (defun random-position () (1+ (random 64)))
...
| I'll grant you that this is even better than my original one.
| (defun random-pos (&optional (bound 1.0))
|    (loop collect (random bound) into list
|          until (= 4 (length (setf list (delete-duplicates list :test '=))
|          finally (return list))
| a little more verbose, but same thing.  Plus I can compile it.
+---------------

I'm confused [not a rare event!]. I thought the OP wanted a
"random-position", which implies to me values that can be used
as indices in an array, so don't the values have to be integers?
In which case, the BOUND can never be less than the desired
result length. That is, your code works fine for (RANDOM-POS 1.0),
but loops forever on (RANDOM-POS 1) or 2 or 3.

Anyway, whilst pondering that I realized there's a still faster way to do
this than use DELETE-DUPLICATES, especially for *large* result lengths:

    > (defun random-positions (length &optional (bound length))    
	(assert (or (<= length bound) (and (floatp bound) (plusp bound))))
	(let ((ht (make-hash-table :test #'eql)))
	  (loop for key = (random bound)
		while (< (hash-table-count ht) length)
	    do (setf (gethash key ht) nil))
	  (loop for key being the hash-keys of ht collect key))) 

    RANDOM-POSITIONS
    > (compile *)

    RANDOM-POSITIONS
    NIL
    NIL
    > (random-positions 4)

    (1 3 2 0)
    > (random-positions 4)

    (3 2 0 1)
    > (random-positions 20)

    (5 18 12 1 7 15 9 0 19 3 11 14 6 2 10 16 4 8 17 13)
    > (random-positions 20)

    (15 19 12 10 13 8 5 4 18 17 16 0 3 9 11 2 7 14 1 6)
    > (random-positions 6 1.0)

    (0.14457798 0.42955732 0.4225546 0.60270786 0.46022022 0.492818)
    > (random-positions 6 1.0)

    (0.9264666 0.22035849 0.25010324 0.48537517 0.69276094 0.55653465)
    > (setf *print-length* 20)

    20
    > (time (random-positions 10000))

    ; Evaluation took:
    ;   0.03 seconds of real time
    ;   0.026913 seconds of user run time
    ;   0.0 seconds of system run time
    ;   51,637,997 CPU cycles
    ;   0 page faults and
    ;   847,432 bytes consed.
    ; 
    (1483 1614 7412 9981 4944 6153 4549 9992 8890 805 ...)
    > 


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Rob Warnock
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <4pmdnScMgoSRYQbUnZ2dnUVZ_uidnZ2d@speakeasy.net>
Madhu <·······@meer.net> wrote:
+---------------
| * (Rob Warnock) <································@speakeasy.net> :
| | Marco Antoniotti  <·······@gmail.com> wrote:
...
| I suspect both of these functions are likely to produce "less than
| random" results, [although I doubt it would be a concern to the OP] I
| think a small bias is introduced in sampling the uniform random process
| producing the numbers.
+---------------

Indeed. We had a long discussion about that here about three years ago,
and at that time I even observed that:

   The pages <http://www.nist.gov/dads/HTML/fisherYatesShuffle.html>
   and <http://www.nist.gov/dads/HTML/idealRandomShuffle.html>[1] contain
   comments on & references to why you don't want to do this. Briefly;
  
      Note: Attaching random tags then sorting (see permutation)
      may not be a perfect shuffle: if tags may be duplicated, a
      stable sort has a greater chance of producing permutations
      with some items in the original order.

A Fisher-Yates shuffle in an array initialized to (IOTA BOUND)
would be truly random, and if the desired output sequence LENGTH
were close to BOUND it would probably be near-optimal in performance
as well [certainly much better than either Marco's or my functions].

On the other hand, if LENGTH << BOUND, then the non-randomness of
Marco's or my functions due to the chance of duplicates will be small,
and the performance will be much better than Fisher-Yates.


-Rob

[1] Actually, the page I quoted in March 2006 was the following one,
    which has since been replaced by the one referenced above:

      http://www.nist.gov/dads/HTML/perfectShuffle.html

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Pascal J. Bourguignon
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <7c63j82awu.fsf@pbourguignon.anevia.com>
Madhu <·······@meer.net> writes:

> * (Rob Warnock) <································@speakeasy.net> :
> Wrote on Wed, 18 Feb 2009 05:22:53 -0600:
> | Marco Antoniotti  <·······@gmail.com> wrote:
> | | (defun random-pos (&optional (bound 1.0))
> | |    (loop collect (random bound) into list
> | |          until (= 4 (length (setf list (delete-duplicates list :test '=))
> | |          finally (return list))
> |
> |
> |     > (defun random-positions (length &optional (bound length))    
> | 	(assert (or (<= length bound) (and (floatp bound) (plusp bound))))
> | 	(let ((ht (make-hash-table :test #'eql)))
> | 	  (loop for key = (random bound)
> | 		while (< (hash-table-count ht) length)
> | 	    do (setf (gethash key ht) nil))
> | 	  (loop for key being the hash-keys of ht collect key))) 
>
> I suspect both of these functions are likely to produce "less than
> random" results, [although I doubt it would be a concern to the OP] I
> think a small bias is introduced in sampling the uniform random process
> producing the numbers.

It eats entropy and takes time, but I don't see how it would introduce
any bias.  


Otherwise, if you need a real time solution, or to spare entropy, you
need to enumerate the results and directly choose one of them:

(defun random-positions (length bound)
  (assert (<= length bound))
  (loop
     :with result = (random (loop :for i :from bound :by -1 :repeat length
                               :for count = bound :then (* count bound) 
                               :finally (return count)))
     :with got = (make-array bound :element-type 'bit :initial-element 0)
     :repeat length
     :for modulo :from bound :by -1
     :for (next item) = (multiple-value-list (floor result modulo))
     :then              (multiple-value-list (floor next   modulo))
     :for previous = 0 :then (mod (1+ slot) bound)
     :for slot = (loop
                    :with count = item
                    :with i = previous
                    :while (or (plusp count) (plusp (aref got i)))
                    :do (if (plusp (aref got i))
                            (setf i (mod (1+ i) bound))
                            (setf i (mod (1+ i) bound)
                                  count (1- count)))
                    :finally (setf (aref got i) 1) (return i))
     :collect slot))

(random-positions 4 64) --> (58 26 37 51)
     

-- 
__Pascal Bourguignon__
From: smallpond
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <d43fab6c-1ebb-4100-bb08-71f9bfc76c48@p36g2000prp.googlegroups.com>
On Feb 18, 7:21 am, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> Madhu <·······@meer.net> writes:
> > * (Rob Warnock) <································@speakeasy.net> :
> > Wrote on Wed, 18 Feb 2009 05:22:53 -0600:
> > | Marco Antoniotti  <·······@gmail.com> wrote:
> > | | (defun random-pos (&optional (bound 1.0))
> > | |    (loop collect (random bound) into list
> > | |          until (= 4 (length (setf list (delete-duplicates list :test '=))
> > | |          finally (return list))
> > |
> > |
> > |     > (defun random-positions (length &optional (bound length))    
> > |  (assert (or (<= length bound) (and (floatp bound) (plusp bound))))
> > |  (let ((ht (make-hash-table :test #'eql)))
> > |    (loop for key = (random bound)
> > |          while (< (hash-table-count ht) length)
> > |      do (setf (gethash key ht) nil))
> > |    (loop for key being the hash-keys of ht collect key)))
>
> > I suspect both of these functions are likely to produce "less than
> > random" results, [although I doubt it would be a concern to the OP] I
> > think a small bias is introduced in sampling the uniform random process
> > producing the numbers.
>
> It eats entropy and takes time, but I don't see how it would introduce
> any bias.  
>
> Otherwise, if you need a real time solution, or to spare entropy, you
> need to enumerate the results and directly choose one of them:
>
> (defun random-positions (length bound)
>   (assert (<= length bound))
>   (loop
>      :with result = (random (loop :for i :from bound :by -1 :repeat length
>                                :for count = bound :then (* count bound)
>                                :finally (return count)))
>      :with got = (make-array bound :element-type 'bit :initial-element 0)
>      :repeat length
>      :for modulo :from bound :by -1
>      :for (next item) = (multiple-value-list (floor result modulo))
>      :then              (multiple-value-list (floor next   modulo))
>      :for previous = 0 :then (mod (1+ slot) bound)
>      :for slot = (loop
>                     :with count = item
>                     :with i = previous
>                     :while (or (plusp count) (plusp (aref got i)))
>                     :do (if (plusp (aref got i))
>                             (setf i (mod (1+ i) bound))
>                             (setf i (mod (1+ i) bound)
>                                   count (1- count)))
>                     :finally (setf (aref got i) 1) (return i))
>      :collect slot))
>
> (random-positions 4 64) --> (58 26 37 51)
>
> --
> __Pascal Bourguignon__
>

Using make-isequence defined above, it's simpler to replace
each selected element with the last element, then select
from the shorter array.

(defun random-positions (length bound)
  (let ((a (make-isequence bound)) (r nil))
    (assert (<= length bound))
    (loop for i from 0 to (1- length)
          collect (aref a (setq r (random (- bound  i))))
          do (setf (aref a r) (aref a (- bound i 1))))))
From: Marco Antoniotti
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <fe04546f-f5aa-4ba1-93f9-8315c0629f74@e18g2000yqo.googlegroups.com>
On Feb 18, 12:22 pm, ····@rpw3.org (Rob Warnock) wrote:
> Marco Antoniotti  <·······@gmail.com> wrote:
> +---------------
> | "William James" <> wrote:
> | > Marco Antoniotti wrote:
> | > > On Feb 17, 4:02 pm, david <······@gmail.com> wrote:
> | > > > i would like to get a list of 4 different random numbers.
> | > > > (wk wn wb bk)
> | > > > i started this code :
> | > > > (defun random-position () (1+ (random 64)))
> ...
> | I'll grant you that this is even better than my original one.
> | (defun random-pos (&optional (bound 1.0))
> |    (loop collect (random bound) into list
> |          until (= 4 (length (setf list (delete-duplicates list :test '=))
> |          finally (return list))
> | a little more verbose, but same thing.  Plus I can compile it.
> +---------------
>
> I'm confused [not a rare event!]. I thought the OP wanted a
> "random-position", which implies to me values that can be used
> as indices in an array, so don't the values have to be integers?
> In which case, the BOUND can never be less than the desired
> result length. That is, your code works fine for (RANDOM-POS 1.0),
> but loops forever on (RANDOM-POS 1) or 2 or 3.

Yes. Of course....  My function above is just a hack (do you have the
Ruby Guy in your killfile? :) ).

Yours is better.

Cheers
--
Marco





>
> Anyway, whilst pondering that I realized there's a still faster way to do
> this than use DELETE-DUPLICATES, especially for *large* result lengths:
>
>     > (defun random-positions (length &optional (bound length))    
>         (assert (or (<= length bound) (and (floatp bound) (plusp bound))))
>         (let ((ht (make-hash-table :test #'eql)))
>           (loop for key = (random bound)
>                 while (< (hash-table-count ht) length)
>             do (setf (gethash key ht) nil))
>           (loop for key being the hash-keys of ht collect key)))
>
>     RANDOM-POSITIONS
>     > (compile *)
>
>     RANDOM-POSITIONS
>     NIL
>     NIL
>     > (random-positions 4)
>
>     (1 3 2 0)
>     > (random-positions 4)
>
>     (3 2 0 1)
>     > (random-positions 20)
>
>     (5 18 12 1 7 15 9 0 19 3 11 14 6 2 10 16 4 8 17 13)
>     > (random-positions 20)
>
>     (15 19 12 10 13 8 5 4 18 17 16 0 3 9 11 2 7 14 1 6)
>     > (random-positions 6 1.0)
>
>     (0.14457798 0.42955732 0.4225546 0.60270786 0.46022022 0.492818)
>     > (random-positions 6 1.0)
>
>     (0.9264666 0.22035849 0.25010324 0.48537517 0.69276094 0.55653465)
>     > (setf *print-length* 20)
>
>     20
>     > (time (random-positions 10000))
>
>     ; Evaluation took:
>     ;   0.03 seconds of real time
>     ;   0.026913 seconds of user run time
>     ;   0.0 seconds of system run time
>     ;   51,637,997 CPU cycles
>     ;   0 page faults and
>     ;   847,432 bytes consed.
>     ;
>     (1483 1614 7412 9981 4944 6153 4549 9992 8890 805 ...)
>     >
>
> -Rob
>
> -----
> Rob Warnock                     <····@rpw3.org>
> 627 26th Avenue                 <URL:http://rpw3.org/>
> San Mateo, CA 94403             (650)572-2607
From: André Thieme
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <gnhh0p$p35$1@news.motzarella.org>
William James schrieb:
> Marco Antoniotti wrote:
> 
>> On Feb 17, 4:02 pm, david <······@gmail.com> wrote:
>>> i would like to get a list of 4 different random numbers.
>>> (wk wn wb bk)
>>> i started this code :
>>>
>>> (defun random-position () (1+ (random 64)))
>>>
>>> (defparameter wk (random-position))
>>>
>>> (excl:until (not (= wb wk)) (setf wb (random-position)))
>>>
>>> but it is not working. i just need to ensure that none of
>>> the positions
>>> are the same. please to help.
>>>
>>> thanks, david
>> Others have already commented on your code.  It is essentially
>> equivalent to the SLDJ code hereafter.
>>
>>   def randomPos () return 1 + random(64) end;
>>
>>   wk = randomPos();
>>
>>   while (wb == wk) wb = randomPos() end;
>>
>> So, you should understand while it is not working...
>>
>> My CL version is
>>
>> (defun random-pos (&optional (bound 64))
>>    (loop for rps = (loop repeat 4 collect (random bound))
>>          until (apply '/= rps)
>>          finally (return rps)))
>>
>> Yes.  The above is potentially very wasteful, but I just
>> figured out that the chance of generating two or more equal
>> numbers gets small for large BOUND.
>>
>> There is probably a nice exercise in probabilistic algorithms
>> in the code snippet above :)  Of course if you replace 4 with
>> a parameter N and make N close to (or larger than) BOUND....
>> But then again, you can pretty safely call RANDOM-POS as
>>
>> prompt> (random-pos 1.0)
>>
>> Ah....  the number of positions you can ask for must also be
>> less than CALL-ARGUMENTS-LIMIT with the code above.
>>
>> Cheers
> 
> Ruby:
> 
> 
> def random_4 ceiling = 64
>   list = []
>   ((list << rand(ceiling)).uniq!) until 4 == list.size
>   list
> end

Clojure:

(defn random-pos [n up-to]
   (take n (distinct (repeatedly #(rand-int up-to)))))


user=> (random-pos 4 20)
(8 15 3 13)
user=> (random-pos 4 20)
(14 7 6 16)
user=> (random-pos 4 30)
(1 10 29 3)


Andr�
-- 
Lisp is not dead. It�s just the URL that has changed:
http://clojure.org/
From: William James
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <gnj2e601vsm@enews2.newsguy.com>
Andr� Thieme wrote:

> Clojure:
> 
> (defn random-pos [n up-to]
>   (take n (distinct (repeatedly #(rand-int up-to)))))
> 
> 
> user=> (random-pos 4 20)
> (8 15 3 13)
> user=> (random-pos 4 20)
> (14 7 6 16)
> user=> (random-pos 4 30)
> (1 10 29 3)

Is there some sort of laziness here?  How does "repeatedly"
know when to quit?
From: Marco Antoniotti
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <5233a757-1bc1-4ed9-af73-c54d4ede21ed@f3g2000yqf.googlegroups.com>
On Feb 19, 8:40 am, "William James" <·········@yahoo.com> wrote:
> André Thieme wrote:
> > Clojure:
>
> > (defn random-pos [n up-to]
> >   (take n (distinct (repeatedly #(rand-int up-to)))))
>
> > user=> (random-pos 4 20)
> > (8 15 3 13)
> > user=> (random-pos 4 20)
> > (14 7 6 16)
> > user=> (random-pos 4 30)
> > (1 10 29 3)
>
> Is there some sort of laziness here?  How does "repeatedly"
> know when to quit?

CL-USER 65 > (lazy-seq:take 4 (lazy-seq:distinct (lazy:repeatedly
'random 64)))
(54 51 13 30)

You must be CLAZY not to use this stuff!  Do it in Ruby!

Cheers
--
Marco
From: Raffael Cavallaro
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <gnjpte$jg$1@aioe.org>
On 2009-02-19 02:40:54 -0500, "William James" <·········@yahoo.com> said:

> Is there some sort of laziness here?

yes:

"(repeatedly f)
Takes a function of no args, presumably with side effects, and returns 
an infinite lazy sequence of calls to it"

-- 
Raffael Cavallaro, Ph.D.
From: André Thieme
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <gnkdan$bql$1@news.motzarella.org>
William James schrieb:
> Andr� Thieme wrote:
> 
>> Clojure:
>>
>> (defn random-pos [n up-to]
>>   (take n (distinct (repeatedly #(rand-int up-to)))))
>>
>>
>> user=> (random-pos 4 20)
>> (8 15 3 13)
>> user=> (random-pos 4 20)
>> (14 7 6 16)
>> user=> (random-pos 4 30)
>> (1 10 29 3)
> 
> Is there some sort of laziness here?  How does "repeatedly"
> know when to quit?

repeatedly produces an infinite sequence of random numbers and
does so lazily. It means (def x (repeatedly #(rand-int 100)))
immediately returns, it takes just a few microseconds.
Only when you want to take stuff out of it the rand-int really
does run, and not before.
Just don�t now type    x   in the repl :)
The repl is eager and greedy, it wants it all. So Clojure would
run a few minutes until it tells you that the JVM is out of
memory.
def is safe though, because it returns its first argument.


distinct is also lazy, and will produce a new (possibly
infinite) sequence. It makes sure that only objects can go into
that sequence which have not went in there before.
But as it is lazy it also does not do anything spectecular.

Now the take comes. It is also lazy and will not move anything
out of the sequence produced by distinct.
But if we type it on the repl, then it will greedily try to eat
as much as take would deliver. I programmed take to give us
max 4 results. This kicks of taking stuff out of distinct, which
will kick off taking stuff out of repeatedly, which will kick off
running rand-int a few times.

Functions that work on lazy sequences can stack so to speak.

If you wish to try out more about Clojure, then you could download
NetBeans 6.5 and install this plugin:
http://enclojure.org/

It�s good to see you beginning doing on-topic posts.


Andr�
-- 
Lisp is not dead. It�s just the URL that has changed:
http://clojure.org/
From: William James
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <gnkgqf0h3@enews2.newsguy.com>
Andr� Thieme wrote:

> It�s good to see you beginning doing on-topic posts.

Nothing is off-topic in c.l.l.
From: Marco Antoniotti
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <2924e5b4-c523-4b78-8815-17409ef24623@x9g2000yqk.googlegroups.com>
On Feb 19, 9:52 pm, "William James" <> wrote:
> André Thieme wrote:
> > It’s good to see you beginning doing on-topic posts.
>
> Nothing is off-topic in c.l.l.

I agree.  But seeing the Ruby implementation of lazy constructs...
that we have not seen yet!

Cheers
--
Marco
From: Scott
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <4d22d7dc-ff37-49eb-883a-470b72086904@v31g2000vbb.googlegroups.com>
On Feb 20, 1:46 am, Marco Antoniotti <·······@gmail.com> wrote:
>
> I agree.  But seeing the Ruby implementation of lazy constructs...
> that we have not seen yet!
>

You say that like you don't think it's possible.  I don't see how you
can know what Common Lisp is *really* better at when you don't know
about anything else.  Ruby stole generators from Python who stole them
from Icon.  I don't know if Icon carried them forward from SNOBOL, or
something else.  Here is a Python version:

    list(islice(distinct(repeatedly(randint, 1, 64)), 4))


Here are the supporting routines:

def repeatedly(callable, *args):
    while 1:
        yield callable(*args)

def distinct(generator):
    hash = {}
    for item in generator:
        if hash.get(item) is None:
            hash[item] = 1
            yield item

from itertools import islice
from random import randint

print list(islice(distinct(repeatedly(randint, 1, 64)), 4))
From: Marco Antoniotti
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <143640e5-767a-4050-87b9-807e541ee7cd@x38g2000yqj.googlegroups.com>
On Feb 20, 8:26 pm, Scott <·······@gmail.com> wrote:
> On Feb 20, 1:46 am, Marco Antoniotti <·······@gmail.com> wrote:
>
>
>
> > I agree.  But seeing the Ruby implementation of lazy constructs...
> > that we have not seen yet!
>
> You say that like you don't think it's possible.

I know it's possible...  I just have not seen the Ruby Guy doing it :)

> I don't see how you
> can know what Common Lisp is *really* better at when you don't know
> about anything else.  Ruby stole generators from Python who stole them
> from Icon.  I don't know if Icon carried them forward from SNOBOL, or
> something else.

IIRC, Icon did get them from SNOBOL.

> Here is a Python version:
>
>     list(islice(distinct(repeatedly(randint, 1, 64)), 4))
>
> Here are the supporting routines:
>
> def repeatedly(callable, *args):
>     while 1:
>         yield callable(*args)
>
> def distinct(generator):
>     hash = {}
>     for item in generator:
>         if hash.get(item) is None:
>             hash[item] = 1
>             yield item
>
> from itertools import islice
> from random import randint
>
> print list(islice(distinct(repeatedly(randint, 1, 64)), 4))

Of course this works perfectly when you want to have *one* form of
suspension of the computation; and it works perfectly in *this* type
of cases (the OP problem and variations thereof).  But generators via
'yield' are not a substitute for lazy evaluation (and the converse
probably holds as well; I don't know).


Cheers
--
Marco
From: Scott
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <428fc9f0-b255-4df2-9b02-73243a9e1143@u18g2000vbb.googlegroups.com>
On Feb 20, 1:36 pm, Marco Antoniotti <·······@gmail.com> wrote:
>
> I know it's possible...  I just have not seen the Ruby Guy doing it :)
>

Fair enough.  I liked him better before I saw all the "Commune Lisp"
silliness.  If he would have just kept posting snippets of Ruby, he
would have been my favorite troll (even above Kenny).  He seems pretty
angry though, so by all means - please taunt away.
From: Marco Antoniotti
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <a6f2d4b4-6cf3-4722-b7ac-97f7b15c6e5e@x10g2000yqk.googlegroups.com>
On Feb 19, 8:52 pm, André Thieme <address.good.until.
···········@justmail.de> wrote:
> William James schrieb:
>
>
>
> > André Thieme wrote:
>
> >> Clojure:
>
> >> (defn random-pos [n up-to]
> >>   (take n (distinct (repeatedly #(rand-int up-to)))))
>
> >> user=> (random-pos 4 20)
> >> (8 15 3 13)
> >> user=> (random-pos 4 20)
> >> (14 7 6 16)
> >> user=> (random-pos 4 30)
> >> (1 10 29 3)
>
> > Is there some sort of laziness here?  How does "repeatedly"
> > know when to quit?
>
> repeatedly produces an infinite sequence of random numbers and
> does so lazily. It means (def x (repeatedly #(rand-int 100)))
> immediately returns, it takes just a few microseconds.


Even less than that.
CL-USER 9 > (time (progn (setq randoms (lazy:repeatedly 'random
1.0d0)) t))
Timing the evaluation of (PROGN (SETQ RANDOMS (REPEATEDLY (QUOTE
RANDOM) 1.0D0)) T)
User time    =        0.000
System time  =        0.000
Elapsed time =        0.000
Allocation   = 336 bytes
0 Page faults
T

> Only when you want to take stuff out of it the rand-int really
> does run, and not before.
> Just don’t now type    x   in the repl :)
> The repl is eager and greedy, it wants it all. So Clojure would
> run a few minutes until it tells you that the JVM is out of
> memory.
> def is safe though, because it returns its first argument.

Should I drop the (PROGN ... T) ?

> distinct is also lazy, and will produce a new (possibly
> infinite) sequence. It makes sure that only objects can go into
> that sequence which have not went in there before.
> But as it is lazy it also does not do anything spectecular.

CL-USER 36 > (time (progn (setq drandoms (lazy-seqs:distinct randoms))
t))
Timing the evaluation of (PROGN (SETQ DRANDOMS (LAZY-SEQS:DISTINCT
RANDOMS)) T)
User time    =        0.000
System time  =        0.000
Elapsed time =        0.000
Allocation   = 416 bytes
0 Page faults
T

> Now the take comes. It is also lazy and will not move anything
> out of the sequence produced by distinct.
> But if we type it on the repl, then it will greedily try to eat
> as much as take would deliver. I programmed take to give us
> max 4 results. This kicks of taking stuff out of distinct, which
> will kick off taking stuff out of repeatedly, which will kick off
> running rand-int a few times.
> Functions that work on lazy sequences can stack so to speak.

CL-USER 41 > (time (lazy-seqs:take 4 drandoms))
Timing the evaluation of (TAKE 4 DRANDOMS)
User time    =        0.000
System time  =        0.000
Elapsed time =        0.000
Allocation   = 1548 bytes
0 Page faults
(0.899690255730395D0 0.06976008653908528D0 0.5055849741583156D0
0.5967178356316605D0)
CL-USER 42 >


> André
> --
> Lisp is not dead. It’s just the URL that has changed:http://clojure.org/

Lisp is not dead. It just added another URL, http://within-parens.blogspot.com

Cheers
--
Marco
From: Nicolas Neuss
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <87k57l9tw3.fsf@ma-patru.mathematik.uni-karlsruhe.de>
Marco Antoniotti <·······@gmail.com> writes:

> CL-USER 41 > (time (lazy-seqs:take 4 drandoms))
> [...]
> Lisp is not dead. It just added another URL, http://within-parens.blogspot.com

Hi Marco,

I wanted to try out CLAZY, but did not find installation instructions on
the clazy homepage.  It would be nice, if you could add those.

Thanks, Nicolas
From: =?UTF-8?B?QW5kcsOpIFRoaWVtZQ==?=
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <gnn35a$5ia$1@news.motzarella.org>
Marco Antoniotti schrieb:
> On Feb 19, 8:52 pm, André Thieme <address.good.until.
> ···········@justmail.de> wrote:
>> William James schrieb:
>>
>>
>>
>>> André Thieme wrote:
>>>> Clojure:
>>>> (defn random-pos [n up-to]
>>>>   (take n (distinct (repeatedly #(rand-int up-to)))))
>>>> user=> (random-pos 4 20)
>>>> (8 15 3 13)
>>>> user=> (random-pos 4 20)
>>>> (14 7 6 16)
>>>> user=> (random-pos 4 30)
>>>> (1 10 29 3)
>>> Is there some sort of laziness here?  How does "repeatedly"
>>> know when to quit?
>> repeatedly produces an infinite sequence of random numbers and
>> does so lazily. It means (def x (repeatedly #(rand-int 100)))
>> immediately returns, it takes just a few microseconds.
> 
> Even less than that.
> 
> CL-USER 9 > (time (defvar randoms (lazy:repeatedly 'random 1.0d0)
> Timing the evaluation of (PROGN (SETQ RANDOMS (REPEATEDLY (QUOTE
> RANDOM) 1.0D0)) T)
> 
> User time    =        0.000
> System time  =        0.000
> Elapsed time =        0.000
> Allocation   = 336 bytes
> 0 Page faults
> RANDOMS

Probably just a minor misunderstanding.
I wrote microseconds, as in „millionth parts of a second”,
not milliseconds.


Anyway, looks nice.
I hope that all CL users will install it, so if they feel the need to
use it, it’ll be there.
As Nicolas suggested, some installation instructions would be nice,
because I would also like to have them :)

On my machine it takes +/- 8 μsecs per
(def x (take 4 (distinct (repeatedly #(rand-int 20)))))


> Lisp is not dead. It just added another URL http://within-parens.blogspot.com

Excellent, and good name for the blog as well.
Thanks Marco, I like it!


André
-- 
Lisp is not dead. It’s just the URL that has changed:
http://clojure.org/
From: John Thingstad
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <op.uph5wigiut4oq5@pandora.alfanett.no>
P� Tue, 17 Feb 2009 16:02:27 +0100, skrev david <······@gmail.com>:

> i would like to get a list of 4 different random numbers.
> (wk wn wb bk)
> i started this code :
>
> (defun random-position () (1+ (random 64)))
>
> (defparameter wk (random-position))
>
> (excl:until (not (= wb wk)) (setf wb (random-position)))
>
> but it is not working. i just need to ensure that none of the
> positions
> are the same. please to help.
>
> thanks, david

(defun shuffle (array)
   (let ((size (array-total-size array)))
     (dotimes (i size)
       (setf (aref array i) (+ i (random (- size i)))))
     array))

(defun make-isequence (size)
   (let ((array (make-array size :element-type 'fixnum)))
     (dotimes (i size array)
       (setf (aref array i) i))))

(defun pick-elements (number total)
   (coerce (subseq (shuffle (make-isequence total)) 0 number) 'list))

and write

(mapcar #'1+ (pick-elements 4 64))

(The 1+ is only neccesary if you insist on mapping from 1..64 personally I  
would use 0..63)

--------------
John Thingstad
From: Pascal J. Bourguignon
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <7cskmd2fzz.fsf@pbourguignon.anevia.com>
david <······@gmail.com> writes:

> i would like to get a list of 4 different random numbers.
> (wk wn wb bk)
> i started this code :
>
> (defun random-position () (1+ (random 64)))
>
> (defparameter wk (random-position))
>
> (excl:until (not (= wb wk)) (setf wb (random-position)))
>
> but it is not working. i just need to ensure that none of the
> positions
> are the same. please to help.
>
> thanks, david


    (do ((results '()                results)
         (alea     (random-position) (random-position)))
        ((<= 4 (length results)) 
         results)
      (pushnew alea results))

or:     

    (loop 
        :with results = '()
        :for alea = (random-position)
        :while (< (length results) 4)
        :do (pushnew alea results)
        :finally (return results))


-- 
__Pascal Bourguignon__
From: david
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <9f307bbd-4483-45df-82c7-68e1566cc192@w35g2000yqm.googlegroups.com>
thanks everyone for your help.
i was not aware of "pushnew"
it seems to be exactly what i need.
please, what is the variable alea?
i mean, why that name?
i will attempt to wrap my brain around
pick-elements.

thanks, david
From: Pascal J. Bourguignon
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <87ljs4lqnz.fsf@galatea.local>
david <······@gmail.com> writes:

> thanks everyone for your help.
> i was not aware of "pushnew"
> it seems to be exactly what i need.
> please, what is the variable alea?
> i mean, why that name?

It means "fate".  ALEA IACTA EST.
In French, it means "hasard", "random".
French "al�atoire" means "random".

-- 
__Pascal Bourguignon__
From: david
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <18dfaae2-e0bb-4ae4-9508-eb964cc59d14@h5g2000yqh.googlegroups.com>
merci beaucoup :)

>
> It means "fate".  ALEA IACTA EST.
> In French, it means "hasard", "random".
> French "aléatoire" means "random".
>
> --
> __Pascal Bourguignon__
From: Nicolas Neuss
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <87y6w4t0mi.fsf@ma-patru.mathematik.uni-karlsruhe.de>
···@informatimago.com (Pascal J. Bourguignon) writes:

> david <······@gmail.com> writes:
>
>> thanks everyone for your help.
>> i was not aware of "pushnew"
>> it seems to be exactly what i need.
>> please, what is the variable alea?
>> i mean, why that name?
>
> It means "fate".  ALEA IACTA EST.
> In French, it means "hasard", "random".
> French "al�atoire" means "random".

? It is latin for "dice", see http://en.wikipedia.org/wiki/Alea_iacta_est.

Nicolas
From: Kojak
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <20090218143736.60e8b0c2@thor.janville.org>
Le Wed, 18 Feb 2009 13:01:25 +0100,
Nicolas Neuss a écrit :

> ···@informatimago.com (Pascal J. Bourguignon) writes:
> 
> > david <······@gmail.com> writes:
> >
> >> please, what is the variable alea?
> >> i mean, why that name?
> >
> > It means "fate".  ALEA IACTA EST.
> > In French, it means "hasard", "random".
> > French "aléatoire" means "random".
> 
> ? It is latin for "dice", see
> http://en.wikipedia.org/wiki/Alea_iacta_est.

Indeed, in latin. But in France one speaks french,
not latin... ;-)

-- 
Jacques.
From: Marco Antoniotti
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <1bb194da-d2a0-4bd7-b5d4-d93816c0cfb3@d32g2000yqe.googlegroups.com>
On Feb 18, 2:37 pm, Kojak <·······@janville.Borg.invalid> wrote:
> Le Wed, 18 Feb 2009 13:01:25 +0100,
> Nicolas Neuss a écrit :
>
> > ····@informatimago.com (Pascal J. Bourguignon) writes:
>
> > > david <······@gmail.com> writes:
>
> > >> please, what is the variable alea?
> > >> i mean, why that name?
>
> > > It means "fate".  ALEA IACTA EST.
> > > In French, it means "hasard", "random".
> > > French "aléatoire" means "random".
>
> > ? It is latin for "dice", see
> >http://en.wikipedia.org/wiki/Alea_iacta_est.
>
> Indeed, in latin. But in France one speaks french,
> not latin... ;-)

I am sure Asterix and Obelix did not originally speak "French" (or,
for sure, Latin)! :)

Cheers
--
Marco
From: Pascal J. Bourguignon
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <7c1vtv3l54.fsf@pbourguignon.anevia.com>
Nicolas Neuss <········@math.uni-karlsruhe.de> writes:

> ···@informatimago.com (Pascal J. Bourguignon) writes:
>
>> david <······@gmail.com> writes:
>>
>>> thanks everyone for your help.
>>> i was not aware of "pushnew"
>>> it seems to be exactly what i need.
>>> please, what is the variable alea?
>>> i mean, why that name?
>>
>> It means "fate".  ALEA IACTA EST.
>> In French, it means "hasard", "random".
>> French "al�atoire" means "random".
>
> ? It is latin for "dice", see http://en.wikipedia.org/wiki/Alea_iacta_est.

Yes.  It is usually translated in French as � Le sort en est jet� �, but
latin term means both dice and chance, hazard.
http://www.online-dictionary.biz/latin/english/vocabulary/reference/alea.asp
In French we distinguish the object from its function, unless we apply
a metonymy.  (That said, � Jeter un sort � means something quite
different, with  � sort � meaning here � sortil�ge � instead of
� hasard � or "fate"...).

-- 
__Pascal Bourguignon__
From: Kaz Kylheku
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <20090225101253.675@gmail.com>
On 2009-02-18, Nicolas Neuss <········@math.uni-karlsruhe.de> wrote:
> ···@informatimago.com (Pascal J. Bourguignon) writes:
>
>> david <······@gmail.com> writes:
>>
>>> thanks everyone for your help.
>>> i was not aware of "pushnew"
>>> it seems to be exactly what i need.
>>> please, what is the variable alea?
>>> i mean, why that name?
>>
>> It means "fate".  ALEA IACTA EST.
>> In French, it means "hasard", "random".
>> French "aléatoire" means "random".
>
> ? It is latin for "dice", see http://en.wikipedia.org/wiki/Alea_iacta_est.

We have the word "aleatory" in English, which anyone halfway literate will
connect with the variable name "alea". :)

Used in music also: John Cage "composed" aleatory music.
From: david
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <01111d0e-9ca7-45c6-adb2-ad66c56347a5@d32g2000yqe.googlegroups.com>
On Feb 18, 12:58 pm, Kaz Kylheku <········@gmail.com> wrote:

>
> We have the word "aleatory" in English, which anyone halfway literate will
> connect with the variable name "alea". :)
>

i had thought i was about 5/8 literate. perhaps i was mistaken.
From: knobo
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <922ee14a-9e8a-43bc-8c31-547210143e76@j8g2000yql.googlegroups.com>
On Feb 17, 5:19 pm, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> david <······@gmail.com> writes:
> > i would like to get a list of 4 different random numbers.
> > (wk wn wb bk)
> > i started this code :
>
> > (defun random-position () (1+ (random 64)))
>
> > (defparameter wk (random-position))
>
> > (excl:until (not (= wb wk)) (setf wb (random-position)))
>
> > but it is not working. i just need to ensure that none of the
> > positions
> > are the same. please to help.
>
> > thanks, david
>
>     (do ((results '()                results)
>          (alea     (random-position) (random-position)))
>         ((<= 4 (length results))
>          results)
>       (pushnew alea results))
>
> or:    
>
>     (loop
>         :with results = '()

Are there any reasons why you use the literal '() here? I would be
careful with that, as I don't understand all consequences. Though I
would assume it work in most cases, as I always used the same notation
for an empty list before, and I always worked. I understand it to the
extent where I can make an example of what does not work, and why. But
I can not explain why this is safe.

>         :for alea = (random-position)
>         :while (< (length results) 4)
>         :do (pushnew alea results)
>         :finally (return results))
>
> --
> __Pascal Bourguignon__
From: Pascal J. Bourguignon
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <7cocwz21me.fsf@pbourguignon.anevia.com>
knobo <······@gmail.com> writes:
>> � � (loop
>> � � � � :with results = '()
>
> Are there any reasons why you use the literal '() here? I would be
> careful with that, as I don't understand all consequences. Though I
> would assume it work in most cases, as I always used the same notation
> for an empty list before, and I always worked. I understand it to the
> extent where I can make an example of what does not work, and why. But
> I can not explain why this is safe.

In addition to Madhu explanations, there are "conotations" to the way
we write nil:

------  ----------  -------------  --------------------------------------------
form    context     evaluates to   means
------  ----------  -------------  --------------------------------------------
'NIL    expression  NIL            the symbol named NIL
NIL     expression  NIL            false or bottom
'()     expression  NIL            empty list
()      source      not evaluated  an empty list of something (eg empty
                                   list of parameters, empty list of slots, etc)
------  ----------  -------------  --------------------------------------------

(Technically, the later is not a 'form', take English meaning of form,
instead of the CLHS Glossary one.)



So for example, we would write:

  (defstruct node validp name children)
  (defun empty-node () (make-node :validp nil :name 'nil :children '()))
  ;;                ^^                    ^^^       ^^^^           ^^^

even if the lisp printer prints NIL for the four cases:

  (list (function-lambda-expression 'empty-node) (empty-node)) 
  --> ((LAMBDA NIL
       ;;      ^^^
          (DECLARE (SYSTEM::IN-DEFUN EMPTY-NODE))
          (BLOCK EMPTY-NODE
             (MAKE-NODE :VALIDP NIL :NAME 'NIL :CHILDREN 'NIL)))
       #S(NODE :VALIDP NIL :NAME NIL :CHILDREN NIL))
       ;;              ^^^       ^^^           ^^^

-- 
__Pascal Bourguignon__
From: Thomas F. Burdick
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <f76e34f1-ac5a-4033-b941-165baaad3f88@s20g2000yqh.googlegroups.com>
On Feb 18, 4:42 pm, ····@informatimago.com (Pascal J. Bourguignon)
wrote:

> ------  ----------  -------------  --------------------------------------------
> form    context     evaluates to   means
> ------  ----------  -------------  --------------------------------------------
> 'NIL    expression  NIL            the symbol named NIL
> NIL     expression  NIL            false or bottom
> '()     expression  NIL            empty list
> ()      source      not evaluated  an empty list of something (eg empty
>                                    list of parameters, empty list of slots, etc)
> ------  ----------  -------------  --------------------------------------------

You don't want to take this reasoning too far and sit around pondering
how you should write nil every time it comes up in your source code,
but thinking a bit about the connotations of how it's written does
make code easier to read sometimes. This is the sort of thing that
comes naturally once you've gotten used to writing Lisp. Or, I should
say the sort of thing that *should* come naturally. One of the
stranger things I've seen in a Lisp was code written like this:

(defclass foo nil
  ...)

(defun foo nil
  (do ((i 0 (+ i 1)))
      nil nil
    ...))

n-i-l instead of () can really throw you for a loop.
From: Pascal J. Bourguignon
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <7chc2q1qw2.fsf@pbourguignon.anevia.com>
"Thomas F. Burdick" <········@gmail.com> writes:

> On Feb 18, 4:42�pm, ····@informatimago.com (Pascal J. Bourguignon)
> wrote:
>
>> ------ �---------- �------------- �--------------------------------------------
>> form � �context � � evaluates to � means
>> ------ �---------- �------------- �--------------------------------------------
>> 'NIL � �expression �NIL � � � � � �the symbol named NIL
>> NIL � � expression �NIL � � � � � �false or bottom
>> '() � � expression �NIL � � � � � �empty list
>> () � � �source � � �not evaluated �an empty list of something (eg empty
>> � � � � � � � � � � � � � � � � � �list of parameters, empty list of slots, etc)
>> ------ �---------- �------------- �--------------------------------------------
>
> You don't want to take this reasoning too far and sit around pondering
> how you should write nil every time it comes up in your source code,
> but thinking a bit about the connotations of how it's written does
> make code easier to read sometimes. This is the sort of thing that
> comes naturally once you've gotten used to writing Lisp. Or, I should
> say the sort of thing that *should* come naturally. One of the
> stranger things I've seen in a Lisp was code written like this:
>
> (defclass foo nil
>   ...)
>
> (defun foo nil
>   (do ((i 0 (+ i 1)))
>       nil nil
>     ...))
>
> n-i-l instead of () can really throw you for a loop.

or:

(defun foo nil
   (do ((i 0 (+ i 1 . nil) . nil))
       ((> i 2 . nil) nil . nil) . nil)
   . nil) ;-)


-- 
__Pascal Bourguignon__
From: Madhu
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <m3tz6rlrzy.fsf@moon.robolove.meer.net>
* knobo Wrote on Wed, 18 Feb 2009 06:37:09 -0800 (PST):
| Are there any reasons why you use the literal '() here? 

(list (eq 'nil nil) (eq nil '())  (eq nil ())) 

=> (T T T)

i.e. the symbol nil, the value nil and the empty list or the literal
empty list are all the same identical common lisp object.

| I would be careful with that, as I don't understand all
| consequences. Though I would assume it work in most cases, as I always
| used the same notation for an empty list before, and I always
| worked. I understand it to the extent where I can make an example of
| what does not work, and why. But I can not explain why this is safe.

If you post your constructed example someone might be able to explain
it.

--
Madhu
From: ····················@hotmail.com
Subject: Re: make a list of different random numbers
Date: 
Message-ID: <2f9e43fb-6501-4df8-b082-f6a64c5685fd@13g2000yql.googlegroups.com>
On 17 Feb, 15:02, david <······@gmail.com> wrote:
> i would like to get a list of 4 different random numbers.
> (wk wn wb bk)
> i started this code :
>
> (defun random-position () (1+ (random 64)))
>
> (defparameter wk (random-position))
>
> (excl:until (not (= wb wk)) (setf wb (random-position)))
>
> but it is not working. i just need to ensure that none of the
> positions
> are the same. please to help.

Chicken Scheme

;; standard (R5RS) scheme doesn't have a random number generator
(define (next-random)   (random 63))

(define (list-of-random n)
    (if (zero? n)
        '()
        (cons (next-random) (list-of-random (- n 1)))))

(define (ran-list)
    (list-of-random 4))