[Haskell-beginners] Random variable holding a function

Ondrej Nekola ondra at nekola.cz
Thu Feb 26 15:48:24 UTC 2015


Thank you, it was very helpfull.

For reference, this is the code, that reflect Daniel's recommendation (I 
hope I made no stupid mistake there):
https://github.com/satai/FrozenBeagle/blob/32f22979d7ef8d5265aa9e49982fa394e2039cf3/src/Population.hs

Thanks
     Ondrej

> On 2015-02-26 at 12:42, Ondrej Nekola <ondra at nekola.cz> wrote:
>> type Selection = Population -> Population
>>
>> so far, it's easy:
>>
>> allSurvive :: Selection
>> allSurvive = id
>>
>> fairChance :: Int -> Population -> RVar Population
>> fairChance newSize p = Population <$> (Data.Random.Extras.sample newSize
>> $ individuals p)
>>
>> 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?
> I would define Selection as
>
> | type Selection = Population -> RVar Population
>
> Then you can compose with >>=, which specializes to
>
> | (>>=) :: RVar Population -> Selection -> RVar Population
>
> or with any of the other Monad operators (eg, >=>).
>
> With this definition, you can keep your current definition of
> fairChance.
>
> There's no problem defining values of type `RVar (Population ->
> Population`.  `fmap allSurvive` is one such.  But defining fairChance
> this way seems a bit awkward to me.
>
> Daniel
>



More information about the Beginners mailing list