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:
<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
.
- References:
- Randomized COND
- From: Mike G.
- Re: Randomized COND
- From: Mike G.
- Randomized COND
- Prev by Date: Re: Randomized COND
- Next by Date: Re: Randomized COND
- Previous by thread: Re: Randomized COND
- Next by thread: Re: Randomized COND
- Index(es):
Relevant Pages
|