[Haskell-beginners] Choosing a function randomly...

edgar klerks edgar.klerks at gmail.com
Sun May 20 22:23:42 CEST 2012


I think the MonadRandom package has a elegant solution for your problem:

fromList :: MonadRandom<http://hackage.haskell.org/packages/archive/MonadRandom/0.1.6/doc/html/Control-Monad-Random-Class.html#t:MonadRandom>m
=> [(a,
Rational<http://hackage.haskell.org/packages/archive/base/4.4.1.0/doc/html/Prelude.html#t:Rational>)]
-> m a

Example:

fromList [(add, 1), (scale, 1), (rareFunction, 0.1)]

On Sun, May 20, 2012 at 8:42 AM, Stuart Hungerford <
stuart.hungerford at gmail.com> wrote:

> Hi,
>
> This is kind-of related to my earlier question on looking up functions
> by name.  Suppose I have a module with a number of functions with the
> same signature:
>
> scale :: Int -> Int -> Int
>
> scale s x = s * x
>
> add :: Int -> Int -> Int
>
> add a x = a + x
>
> ...
>
> I'd like to choose and run one of these functions randomly at run
> time. I can see I could use some kind of case expression:
>
> op :: Int -> Int -> Int
>
> op p x = case random(1,2) of
>   1 -> scale p x
>   2 -> add p x
>
> Or some kind of pattern guards:
>
> op p x
>  | random(1,2) == 1 = scale p x
>  | otherwise  = add p x
>
> Although that method won't work as is for more than two choices.  Are
> these methods the most idiomatic way of randomly choosing a function?
>  How hard would it be to use the machinery of the QuickCheck library
> for this, given it must be doing something similar in test suites?
>
> Thanks,
>
> Stu
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120520/d23fe16e/attachment.htm>


More information about the Beginners mailing list