title: Implementing Haskell's Maybe Monad in c++11
body: <p>I am trying to implement the Maybe monad from Haskell using the lambda functions in C++11 and templates. Here's what I have so far</p>
  
  <pre><code>#include&lt;functional&gt;
  #include&lt;iostream&gt;
  using namespace std;
  
  template&lt;typename T1&gt;
  struct Maybe
  {
    T1 data;
    bool valid;
  };
  
  template&lt;typename T1, typename T2&gt;
  Maybe&lt;T2&gt; operator&gt;&gt;=(Maybe&lt;T1&gt; t, std::function &lt; Maybe&lt;T2&gt; (T1)&gt; &amp;f)
  {
    Maybe&lt;T2&gt; return_value;
    if(t.valid == false)
    {        
      return_value.valid = false;
      return return_value;
    }
    else
    {        
      return f(t.data);
    }            
  }
  
  
  int main()
  {
    Maybe&lt;int&gt; x = {5, true};
    Maybe&lt;int&gt; y = {29, false};
  
    auto z = [](int a) -&gt; Maybe&lt;int&gt;
      {
        Maybe&lt;int&gt; s;
        s.data = a+1;
        s.valid = true;
        return s;
      };
  
    Maybe&lt;int&gt; p = (x &gt;&gt;= z);
    Maybe&lt;int&gt; q = (y &gt;&gt;= z);
  
    cout&lt;&lt;p.data&lt;&lt;' '&lt;&lt;p.valid&lt;&lt;endl;        
    cout&lt;&lt;q.data&lt;&lt;' '&lt;&lt;q.valid&lt;&lt;endl;
  }    
  </code></pre>
  
  <p>When it comes to the actual <code>&gt;&gt;=</code> call, I am getting a compiler error saying that no match found for <code>&gt;&gt;=</code> operator. Is my understanding of C++11's lambda functions failing me here?</p>