ThinkGeek - Cool Stuff for Geeks and Technophiles

Don’t let the name scare you. A monad is just something that represents one or more computations, a workflow. The need for monads came up in pure functional programming languages like Haskell where given a set of inputs, a function should return the same value every time. But what happens when you have a function that takes a filename and line number and returns a byte array of the data on that line in the file? Might it not return a different byte array between subsequent calls if the file changed? The solution: instead of executing the operations to read the file and return the data, the function creates a workflow(monad) that will do that. So what comes back from the function is a workflow, something that represents the operations to read from the file. In typical pragmatic form, Microsoft decided to use a better name for this in F#: Workflows. The stuff in the Workflow is called the compuation expression. These names make sense.

It turns out workflows are useful for other things like building Sequence Expressions and Async Workflows. The advantage is a syntactic sugar for creating what is usually a gobbledygook of disparate pieces of code to accomplish the same thing. You can create your own workflows by creating a type with a couple of important mothods: Bind, Delay, and Return. I really recommend watching Luca Bolognese’s presentation on F#. Its worth watching for the section where he discusses Async Workflows. Pretty amazing stuff.

If you come from the Lisp world, F# Sequence Expressions will seem familiar to you. In Clojure and Lisp they are called List Comprehensions but are essentially a specialized Monad/Workflow.