title: F# Computation Expressions: How to use `for` to return a `seq`?
body: <p>I'm writing a computation expression that is essentially implementing a <strong>State monad</strong> and I'm trying to use <code>for</code> expression.</p>
  <p>I can use the boilerplate function <code>forLoop</code> or even <code>MBuilder.For()</code>, and they all return a nice <code>M&lt;seq&lt;'U&gt;, _&gt;</code> which can be processed by further <code>let!</code> expression. But when I'm trying to do the same with <code>for</code> expression, it fails to compile telling me that the expression inside <code>for</code> must return <code>unit</code>.</p>
  <p>I'm sorry for a large code block that I can't make smaller.</p>
  <pre class="lang-fsharp prettyprint-override"><code>type M&lt;'T, 'E&gt; = 'T * 'E                 // Monadic type is a simple tuple
  type MFunc&lt;'T, 'U, 'E&gt; = 'T -&gt; M&lt;'U, 'E&gt; // A function producing monadic value
  
  // typical boilerplate functions
  let bind (x: M&lt;'T, 'E&gt;) (f: MFunc&lt;'T, 'U, 'E&gt;) : M&lt;'U, 'E&gt; =
      let a, s = x
      let b, s1 = f a
      b, s1 + s
  let combine (e1: M&lt;'T, 'E&gt;) (e2: M&lt;'U, 'E&gt;) : M&lt;'U, 'E&gt; = bind e1 (fun _ -&gt; e2)
  let delay f = (fun () -&gt; f())()
  
  // These two are explained below
  let combineList (e1: M&lt;'T, 'E&gt;) (e2: M&lt;'T seq, 'E&gt;) : M&lt;'T seq, 'E&gt; =
      bind
          e1
          (fun x1 -&gt;
              let e2body, e2state = e2
              seq{yield! e2body; yield x1}, e2state
          )
  let forLoop (xs: seq&lt;'T&gt;) (f: MFunc&lt;'T, 'U, 'E&gt;) : M&lt;seq&lt;'U&gt;, 'E&gt; =
      Seq.fold
          (fun s x -&gt; combineList (f x) s)
          (Seq.empty&lt;'U&gt;, 0)
          xs
  
  // Builder class
  type MBuilder() =
      member this.Bind (x: M&lt;'T, 'E&gt;, f: MFunc&lt;'T, 'U, 'E&gt;) : M&lt;'U, 'E&gt; = bind x f
      member this.Return(a) = a, 0
      member this.Combine(e1,e2) = combine e1 e2
      member this.Delay(f) = delay f
      member this.Zero() = (), 0
      member this.For (xs: seq&lt;'T&gt;, f: MFunc&lt;'T, 'U, 'E&gt; ) : M&lt;seq&lt;'U&gt;, 'E&gt; = forLoop xs f
  let stateful = new MBuilder()
  
  let mTest = stateful {
      // below is the typical use, just for example
      let! var1 = &quot;q&quot;, 3
      let! var2 = true, 4
      // so far so good, the monad returns (&quot;test&quot;, 7)
      return &quot;test&quot;
      }
  </code></pre>
  <p>Now, I'm trying to use the loops. The following three calls work as expected, incrementing the state as many times as there are elements in <code>myList</code>. They also return a beautiful <code>string seq</code>, obviously except the last call:</p>
  <pre class="lang-fsharp prettyprint-override"><code>    let myList = [&quot;one&quot;; &quot;two&quot;; &quot;three&quot;] // define test data
  
      let! var3 = stateful.For(myList, (fun x -&gt; x, 1))
      let! var4 = forLoop myList (fun x -&gt; x, 1)
  
      // No return value, as expected
      for str in myList do
          let! _ = str, 1
          return &quot;&quot;
  </code></pre>
  <p>But the following does not compile: <code>error FS0001: This expression was expected to have type M&lt;'a,int&gt; but here has type unit</code></p>
  <pre class="lang-fsharp prettyprint-override"><code>    let! var5 =
          for str in myList do
              let! _ = str, 1
              return &quot;&quot;
  </code></pre>
  <p>So my question is - <strong>What am I doing wrong</strong>?</p>
  <p>I'm also a bit confused with two overloads of <code>For</code> described <a href="http://msdn.microsoft.com/en-us/library/dd233182.aspx" rel="nofollow noreferrer">here</a> and how to make use of both.</p>