
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. At one extreme, I guess I could use the State monad for everything that has state, right down to the neurons in their brains. (I assume that would be overkill.) At the other extreme, I could do without the State monad altogether, and just have functions that take a creature, let it have an experience, and return a new creature. I guess somewhere in-between there's a happy medium. Perhaps I should us the State monad for the creatures, but not for anything lower-level than that. Or maybe use it for the creatures and their brains. One issue that complicates things slightly is that a *lot* of the processing I'm doing requires random numbers. The neurons don't require any randomness, but the brain as a whole does. And reproduction requires randomness down at the DNA level. The code works, but I feel it's a bit messy because of needing randomness in so many places, and I'd like to clean up the design a bit. Generally speaking, is it best to go for the "bon-bon" approach, with a pure functional core, and a monadic layer on the outside?

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

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).
Thank you so much. The advice and links you gave me are very helpful.
participants (2)
-
Amy de Buitléir
-
Heinrich Apfelmus