F# Either computation expression with while loop I want to implement an Either computation expression in F#. The computation executes a function inside a while loop, and this function's return type is Result&lt;int, string&gt;. The function might return an Error&lt;string&gt; if some condition is met, and then the whole computation expression should be stopped.

This is the code I have so far to illustrate what I am trying to achieve:

type EitherBuilder () =
  member this.Bind(x, f) =
    match x with
    | Ok s -&gt; f s
    | Error f -&gt; Error f
  member this.Return x = Ok(x)
  member this.ReturnFrom x = x
  member this.Zero() = Ok()
  member this.Delay(f) = f
  member this.Combine(x, f) = this.Bind(x, f)
  member this.Run(f) = f()
  member this.While(guard, body) =
    if guard () then this.Bind(body (), fun _ -&gt; this.While(guard, body))
    else this.Zero()

let either = EitherBuilder ()

let mutable i = 0

let func i =
  if i &lt; 3 then Ok(i)
  else Error("error")

let result = either {
  let mutable res = 0
  while i &lt; 10 do 
    let! output = func i
    res &lt;- output
    i &lt;- i + 1
  return res
}


I have tested it and it looks like it is working and doing what I am after (in the example above, the computation is stopped when i becomes 3 and result is set to Error "error"), but I am not 100% sure if the implementation of the EitherBuilder is fully correct. I would appreciate some feedback.
There are a few things I would probably add or change about your implementation.  When I create a new computation builder, I typically put the logic in a module, and then just call the functions in that module from the builder.

Another thing I would do is prefer the use of discriminated unions for Error types.  This makes it possible to match on the error, and also lets you use a little trick with the implementation of delay/run on the computation builder to wrap up any unhandled exceptions that occur inside your computation expression as an Error as well, so you don't have to wrap things in try/with.  

Next, I would implement some common operators like &gt;&gt;=, &lt;*&gt;, and &lt;!&gt;, as well as lift and apply functions.  These will make it easier to work with the Results outside of the computation expression and interface with functions that don't natively work with Results.

Finally, I would implement at least all the standard members for the computation builder, including TryWith/TryFinally/Using/For.  An example implementation might look like the following:

module Either =
    open FSharp.Reflection
    open System

    let inline bind f x =
        match x with
        | Ok s -&gt; f s
        | Error e -&gt; Error e

    let inline ok x = Ok x

    let inline zero () = Ok ()

    let inline convertExceptionToResult&lt;'result,'event&gt; (ex: exn) =
        let union = FSharpType.GetUnionCases(typeof&lt;Result&lt;'result, 'event&gt;&gt;)
        if typeof&lt;'event&gt;.IsAssignableFrom(typeof&lt;exn&gt;)
        then FSharpValue.MakeUnion(union.[1], [|[ex] |&gt; box|]) |&gt; unbox&lt;Result&lt;'result, 'event&gt;&gt;
        elif FSharpType.IsUnion typeof&lt;'event&gt;
        then let cases = FSharpType.GetUnionCases(typeof&lt;'event&gt;)
             match cases |&gt; Seq.tryFind (fun case -&gt; case.GetFields().Length = 1 &amp;&amp; case.GetFields().[0].PropertyType.IsAssignableFrom(typeof&lt;exn&gt;)) with
             | Some case -&gt; FSharpValue.MakeUnion(union.[1], [|[FSharpValue.MakeUnion(case, [|ex |&gt; box|]) |&gt; unbox&lt;'event&gt;] |&gt; box|]) |&gt; unbox&lt;Result&lt;'result, 'event&gt;&gt;
             | None -&gt; failwithf "No Union Case of Event Type %s Supports Construction from an Unhandled Exception: \r\n%O" typeof&lt;'event&gt;.Name ex
        else failwithf "Unable To Construct a Failure of type %s from Unhandled Exception: \r\n%O" typeof&lt;'event&gt;.Name ex

    let inline delay&lt;'result,'event&gt; (f: unit -&gt; Result&lt;'result, 'event&gt;) = fun () -&gt;
        try f()
        with | ex -&gt; convertExceptionToResult ex

    let inline run&lt;'result,'event&gt; (f: unit -&gt; Result&lt;'result, 'event&gt;) =
        try f()
        with | ex -&gt; convertExceptionToResult ex

    let inline (&gt;&gt;=) result f = bind f result

    let inline apply wrappedFunction result = 
        match wrappedFunction, result with
        | Ok a, Ok b -&gt; Ok (a b)
        | Error e, Ok s -&gt; Error e
        | Ok s, Error e -&gt; Error e
        | Error e, Error _ -&gt; Error e

    let inline (&lt;*&gt;) wrappedFunction result = apply wrappedFunction result

    let inline lift f result = apply (ok f) result

    let inline (&lt;!&gt;) f result = lift f result

    type EitherBuilder() = 
        member __.Zero() = zero()
        member __.Bind(x, f) = bind f x
        member __.Return(x) = ok x
        member __.ReturnFrom(x) = x
        member __.Yield(x) = ok x
        member __.YieldFrom(x) = x
        member __.Combine (a, b) = bind b a
        member __.Delay f = delay f
        member __.Run f = run f
        member __.TryWith (body, handler) =
            try body()
            with | ex -&gt; handler ex
        member __.TryFinally (body, compensation) =
            try body()
            finally compensation()
        member this.Using(d:#IDisposable, body) =
            let result = fun () -&gt; body d
            this.TryFinally (result, fun () -&gt;
                match d with
                | null -&gt; ()
                | d -&gt; d.Dispose())
        member this.While (guard, body) =
            if not &lt;| guard () 
            then this.Zero()
            else bind (fun () -&gt; this.While(guard, body)) (body())
        member this.For(s:seq&lt;_&gt;, body) =
            this.Using(s.GetEnumerator(), fun enum -&gt;
                this.While(enum.MoveNext,
                    this.Delay(fun () -&gt; body enum.Current)))

    let either = EitherBuilder()


Looking at your example usage of the EitherBuilder, I would probably change the while loop to a tail-recursive function.  You can still use while loops with the new EitherBuilder if you want, but using a tail-recursive function allows you to eliminate the mutable state, and correctly defining your Error cases as a discriminated union will allow you to access the failing values if you need to without having the global state i.  That would look something like this:

open Either

type Errors =
| OutOfRange of int
| UnhandledException of exn // This is required to allow conversion from Exceptions to Errors

let func i =
  if i &lt; 3 then Ok(i)
  else Error &lt;| OutOfRange i

let result =
    let rec loop i res =
        either {
            if i &lt; 10
            then let! output = func i
                 return! loop (i + 1) output
            else return res
        }
    loop 0 0


In which case, result has the value:

Result&lt;int,Errors&gt; = Error (OutOfRange 3)