Re: Randomized COND
- From: Juho Snellman <jsnell@xxxxxx>
- Date: 7 Aug 2007 21:03:15 GMT
Mike G. <michael.graffam@xxxxxxxxx> wrote:
This works OK. But for large lists of clauses, I end up testing the
number for EQ many, many times. I'd like to just jump to the random
clause, test (optionally perform code) and exit. I have a macro which
does this using EVAL, but that isn't optimal either.
How can I jump to the Nth expression in my code?
One option is to construct a tree of comparisons, rather than linearly
testing all of the options. There's a one implementation of this idea
by Michael Weber at:
Using ECASE/TREE from that page, you could write:
(defmacro rcond (&rest clauses)
`(ecase/tree (random ,(length clauses))
,@(loop for clause in clauses
for i from 0
collect (,i (when ,(car clause)
Another option is to create create a function for each of the rcond
clauses, put those functions into an array, and then (funcall (aref
array (random (length array)))).