[racket] Probability macro/HOF (for use in Typed Racket)
Frequently throughout a program I am working on, I would like to specify a
probabalistic choice between two (for the sake of simplicity) procedures
that might be applied at runtime.
at present, I have defined a function like so :
(: odds-on ((Any * -> Any) (Any * -> Any) Real -> (Any * -> Any)))
(define (odds-on choice alternative odds)
(cond [(< (random) odds) choice]
[else alternative]))
s.t. I could do something like this > ((odds-on common-thing rare-thing
.80) 'an-argument "an argument of different type")
and around 80% of the time it would apply common-thing to the arguments,
and around 20% of the time it would apply rare-thing.
I understand that my type annotation above is wrong for what I want to
do--I want to be able to use odds-on with arbitrary function types and
function arity, while the above is the form for homogenous rest args. I can
also see how this might be an insoluble type conundrum from the
type-checker's point of view (but my understanding of type systems is weak,
so I'm guessing).
I've read over the docs for parameterized types and heterogenous variable
arity rest arguments at least a dozen times, so I'm here looking for
advice! My understanding of the purpose of macros in Racket leads me to
believe that might be worth exploring, but I'm also wary of opening up a
whole other can of worms at this point.
Is it possible to do what I have asked with type checking? Would I better
off abandoning type checking and keeping the syntax? Is there some far
Rackety-er way of achieving this effect?
Thanks for the help, again!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140321/9f2bdf0a/attachment.html>