[Haskell-beginners] Random variable holding a function

Ondrej Nekola ondra at nekola.cz
Thu Feb 26 12:42:03 UTC 2015


Hi

I try to simulate some processes, that contain randomness and I would 
like do it idiomatic way and keep as much code not to know about 
randomness as possible.

I have a following code (simplified to make email more readable):

import Data.List
import Data.Functor
import Genes
import Data.Random.RVar
import Data.Random.Extras

data Individual = Individual (Int, Int) deriving (Eq, Show) -- this part 
is a bit more complicated...

data Population = Population { individuals :: [Individual] } deriving 
(Eq, Show)

type Selection = Population -> Population

so far, it's easy:

allSurvive :: Selection
allSurvive = id

extinction :: Selection
extinction _ = Population []

The issue comes, when I want to have some randomness in the process. I 
am able to write a "pick some individuals into next generation" sort of 
"Selection"

fairChance :: Int -> Population -> RVar Population
fairChance newSize p = Population <$> (Data.Random.Extras.sample newSize 
$ individuals p)

But it obviously doesn't fit into the "Selection" type. I would like to 
have something like

fairChance :: Int -> RVar Selection
(e.g. fairChance :: Int -> RVar (Population -> Population))

Is there a way to do this? Or should I give up this way and try to give 
up some purity and go withtype Selection :: RVar Population -> RVar 
population?

Thanks
    Ondrej


More information about the Beginners mailing list