Chapter 14
The final monad has a string that shows the outcome. It has the point that was
established and the sequence of dice rolls. Each outcome has a specific payout that
we can use to determine the overall fluctuation in the bettor's stake.
We can use simulation to examine different betting strategies. We might be searching
for a way to defeat any house edge built into the game.
There's a small asymmetry in the basic rules of the game. Having 11 as
an immediate winner is balanced by having 3 as an immediate loser.
The fact that 2 and 12 are also losers is the basis of the house's edge of
5.5 percent (1/18 = 5.5) in this game. The idea is to determine which of
the additional betting opportunities will dilute this edge.
A great deal of clever Monte Carlo simulation can be built with a few simple,
functional programming design techniques. The monad, in particular, can help
structure these kinds of calculations when there are complex orders or internal states.
Additional PyMonad features
One of the other features of PyMonad is the confusingly named monoid. This comes
directly from mathematics and it refers to a group of data elements that have an
operator, an identity element, and the group is closed with respect to that operator.
When we think of natural numbers, the add operator, and an identity element 0 , this
is a proper monoid. For positive integers, with an operator *, and an identity value
of 1 , we also have a monoid; strings using | as an operator and an empty string as an
identity element also qualifies.
PyMonad includes a number of predefined monoid classes. We can extend this
to add our own monoid class. The intent is to limit a compiler to certain kinds of
optimizations. We can also use the monoid class to create data structures which
accumulate a complex value, perhaps including a history of previous operations.
Much of this provides insight into functional programming. To paraphrase the
documentation, this is an easy way to learn about functional programming in,
perhaps, a slightly more forgiving environment. Rather than learning an entire
language and toolset to compile and run functional programs, we can just
experiment with interactive Python.
Pragmatically, we don't need too many of these features because Python is already
stateful and offers strict evaluation of expressions. There's no practical reason to
introduce stateful objects in Python, or strictly-ordered evaluation. We can write
useful programs in Python by mixing functional concepts with Python's imperative
implementation. For that reason, we won't delve any more deeply into PyMonad.