computation-expression

Computation Expression doesn't execute Let

I'm using F# v 1.9.6.2, and I've defined a very simple computation expression: type MaybeBuilder() = member this.Let(x, f) = printfn "this.Let: %A" x this.Bind(Some x, f) member this.Bind(x, f) = printfn "this.Bind: %A" x match x with | Some(x) when x >= 0 && x <= 100 -> f(x) | _ -...

Why does this F# computation expression give a warning?

This code: type Result = Success of string type Tracer() = member x.Bind(p: Result, rest: (string -> Result)) = match p with | Success s -> rest s let tracer = new Tracer() let t = tracer { let! x = Success "yes!" let! y = Success "waste of time" return! Success x } printfn "%A" t prints Success "yes!" But gives ...

Custom computation expressions in F#

I've been toying with monads in F# (aka computation expressions) and I wrote this simple Identity monad: type Identity<'a> = | Identity of 'a type IdentityBuilder() = member x.Bind (Identity v) f = f(v) member x.Return v = Identity v let identity = new IdentityBuilder() let getInt() = identity { return Int32.Parse(Conso...

How do you create an F# workflow that enables something like single-stepping?

I'd like to create a builder that builds expressions that returns something like a continuation after each step. Something like this: module TwoSteps = let x = stepwise { let! y = "foo" printfn "got: %A" y let! z = y + "bar" printfn "got: %A" z return z } printfn "two steps" let a = x() printfn "somethin...

LINQ query expressions that operate on types (monads?) other than IEnumerable<T> -- Possible uses?

I'm reading the book Real-world functional programming by Tomas Petricek and Jon Skeet and I'm having a hard time digesting the section on computation expressions1) (aka monads). Through this book, I learnt that contrary to my previous experiences LINQ query expressions aren't restricted to IEnumerable<T>, but can work on other custom...

Recursive functions in computation expressions

Some background first. I am currently learning some stuff about monadic parser combinators. While I tried to transfer the 'chainl1' function from this paper (p. 16-17), I came up with this solution: let chainl1 p op = parser { let! x = p let rec chainl1' (acc : 'a) : Parser<'a> = let p' = parser { let! f = op ...

Recursive computation expressions

In a previous question I was told how to rewrite my computation expressions so it uses tail recursion. I rewrote my code but still got a StackOverflowException. To locate the problem I wrote some small code using a state monad (taken from this blog entry): type State<'a, 's> = State of ('s -> 'a * 's) let runState (State s) initialSta...