I am trying to create an HOF in typed racket which will allow me to express the idea, "at evaluation time, roll the dice and pick the procedure to apply".
Currently, I am working with the following:
(: odds-on ((Any * -> Any) (Any * -> Any) Real -> (Any * -> Any)))
(define (odds-on choice alternative odds)
(cond [(< (random) odds) choice]
[else alternative]))
with the idea being I could use it like this:
> ((odds-on do-a-common-thing do-a-rarer-thing .75) 'x '(a b c) (set z))
where do-a-common-thing
and do-a-rarer-thing
are defined elsewhere, and may have arbitrary argument arity and types. So that about 3/4 of the time the first function would be called, and 1/4 the second would.
But I'm running into type-checking and arity issues left and right... Based on the docs, I need heterogenous rest arguments, but I don't follow the docs very well, not being a scholar of type systems... I simply want to be able to specify two functions and a probability, and have the function which is called be determined at runtime!
And, overall, maybe a macro/syntax form would be a more sensible way to create this effect? From an interface design standpoint, this capability is exactly what I need in my program, but I don't know how to implement it with static types... But I want the static types to help keep a handle on other parts of the program.