[Haskell-cafe] Re: Help mixing pure and IO code
Heinrich Apfelmus
apfelmus at quantentunnel.de
Tue Dec 1 05:33:40 EST 2009
Luke Palmer wrote:
> Hector Guilarte <hectorg87 at gmail.com> wrote:
>>
>> f:: [int] -> (a,[Int])
>> f randomList =
>> let (usedRandomNumber,newRandomList) = g randomList
>> in (usedRandomNumber,newRandomList)
>
> This pattern can be encapsulated in a monad:
>
> newtype RandM a = RandM { unRandM :: [Int] -> (a,[Int]) }
>
> instance Monad RandM where
> [...]
>
> See the similarity?
>
> Of course, there is no need to implement this yourself. It is already
> implemented as State [Int]. And as long as you are doing that, you
> might as well use Rand from the MonadRandom package. In fact, I have
> argued that you should use MonadRandom instead of the lower-level
> System.Random whenever possible:
> http://lukepalmer.wordpress.com/2009/01/17/use-monadrandom/
The rationale being that RandM a has a natural interpretation as
"random variable of type a" with no reference to how it's actually
implemented.
Regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
More information about the Haskell-Cafe
mailing list