Re: Randomized COND



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:
<http://www.foldr.org/~michaelw/log/programming/lisp/icfp-contest-2006-vm>

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)
,@(cdr 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)))).

--
Juho Snellman
.



Relevant Pages

  • Re: An array problem
    ... The order of elements in array A is important. ... where the order of elements in the report is as was in array A. ...    when b.include? ... since when clauses are exclusive. ...
    (comp.lang.ruby)