[Haskell-beginners] Architecture and monads

Heinrich Apfelmus apfelmus at quantentunnel.de
Tue Dec 7 18:31:51 CET 2010


Amy de Buitléir wrote:
> Until recently, the choice of whether or not to use monads was easy. I
> didn't understand them, so I avoided them! But now that I think I
> understand enough to begin to use them, I have a different problem: I
> don't have the experience to know when using them is a good idea, and
> when it's overkill. This is my first serious Haskell project, and I
> would appreciate any advice.
> 
> Here's the scenario: I'm developing a virtual world with alife
> creatures. [...]  One
> issue that complicates things slightly is that a *lot* of the
> processing I'm doing requires random numbers.[...]

My advice is this: instead of using a state monad, use a monad that has
an independent interpretation. (You may *implement* it in terms of the
state monad, but you may not *think* of it as a state monad).

My favorite example is the "random number monad"  Rand . Namely, the
declaration

    x :: Rand a

denotes a random variable  x  of values of type  a . You would usually
implement it as a state monad

    type Rand a = StdGen -> (a,StdGen)

but it is preferable to think of it as a random variable in the
mathematical sense, which is completely independent of any notion of
state. See also

    http://lukepalmer.wordpress.com/2009/01/17/use-monadrandom/
    http://apfelmus.nfshost.com/articles/random-permutations.html


> Generally speaking, is it best to go for the "bon-bon" approach, with
> a pure functional core, and a monadic layer on the outside?

If you cannot find an independent interpretation, the "bon-bon" approach
is usually a good idea.


Regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com




More information about the Beginners mailing list