macro of the day- whichever

I started reading the sources and docs for Alexandria - and stumbled across the odd looking whichever.

It’s a macro that takes a list of forms, and selects only one, at random, from the forms, and evaluates it.

A good example of this is present in the arnesi test suite (alexandria’s own tests just assert that one of a b or c are the result, this incf trick expresses the intent that only one path was evaluated):

I checked the other sources I have - while I can see what this does (and can speculate how you might use this) - the only places I see this exercised (in the software I have downloaded from the quicklisp dist) is in its test suites. It looks like a simple example of an evenly weighted non-deterministic evaluator - I think that this might present a solution for a “choose” or “amb” case.

One thing to note is that the call to setf wraps each possibility in a lambda calling macroexpand, so we can defer expansion. The manual implementation of nth as expand (using bisection) I assume is built around ensuring we’re not evaluating this list - I’m not 100% sure why we can’t do that safely. I would be interested to see how macroexpand looks on this.