Tag Archives: monads

F#: Pipelined Monads – Solutions From the Community

I was actually quite pleased that I got my state/error monad working (F#: Pipelined Monads), but there was a far more elegant solution staring me in the face. Thanks to other members of the F# community, I have reverted my code back to how it was and added two operators. In doing so the code is now far more elegant. Read on to see more, but thanks again to:

Continue reading


F#: Pipelined Monads

Update (12 September 2009): The community has provided a more elegant solution to this problem. See my more recent post for more details here: F# Pipelined Monads – Solutions From the Community. Thanks go to Keith Battocchi and Felix (felixmar on hubFS) for the revised solution. I have left this post unchanged and I advise you to use the revised solution in your own projects.

I recently completed my own construction of a couple of monads that I am using in a mini-project. I like the way that I am able to compose functions returning monadic values by using let! and do! directly. However, I was frustrated by the need to use multiple let! statements when I needed to chain constructs into a pipeline, for example:

let result = myBuilder {
  let result1 = initialValue
  let! r2 = r1 |> f1
  let! r3 = r2 |> f2
  let! r4 = r3 |> f3
  return r4 }

It is important to use let! because each step in the pipeline is allowed to fail. More generally, the behaviour required is that bind must be incorporated into the pipeline. Additionally, the proliferation of intermediary variables greatly increased the possibility of error in my code because I might have referred to one of the intermediate results, rather than the most recent result. Compare this to a non-computation expression version:

let result = 
  |> f1
  |> f2
  |> f3

The challenge before me was could I do any better?

Continue reading

F#: Delayed, Compositional Maybe Monad (Workflow) – Full Source

There are some good examples of the maybe monad in F# based on the option ‘a type. However, this approach has some shortcomings. This is a short post with code that I am using to provide the semantics of the maybe monad in F#.

Continue reading

F#: Custom Monad (Workflow) Development

It is my experience that learning how to develop monads is hard. I could follow the theory, and I thought I could even read example code, but when I attempted to actually write my own then I found it hard to marry the implementation and the theory. If that is you experience, then I hope this post will help a little.

Continue reading

Playing with LINQ Expression Trees

Well, LINQ has been around for a while now and I suppose many of you have been playing with (I mean using) it in both production and hobby code.  One of the core features that LINQ requires is the ability to express lambda expressions as expression trees rather than delegates.  Delegates represent compiled (and thus opaque) code, but expression trees can be manipulated for optimisation and language translation, such as to SQL.

Now LINQ does that all for you, but have you ever wondered whether you can make an expression tree directly?

Continue reading