title: access to intermediary return values in a clojure monad
body: <p>I would like to compose functions of the following type:</p>
  
  <pre><code>; input is a value x and the return value is a vector. 
  (defn f [x] [x *whatever*])
  </code></pre>
  
  <p>In order to compose such functions I use such a monad:</p>
  
  <pre><code>(require '[clojure.algo.monads :as m])
  (m/defmonad example-m
     [m-result (fn [v] v)
      m-bind   (fn [[v k] f]
                (f v) )])
  </code></pre>
  
  <p>Using the following 2 example functions,</p>
  
  <pre><code>(defn f1 [s] [(inc s) :f1])
  (defn f2 [s] [(* 2 s) :f2])
  </code></pre>
  
  <p>evaluating the monad returns the correct value:</p>
  
  <pre><code>(m/domonad example-m [x1 [4 1] 
                        x2 (f1 x1) 
                        x3 (f2 x2) 
                        ] x3) ; =&gt; [10 :f2]
  </code></pre>
  
  <p>However, I am interested to evaluate the following expression:</p>
  
  <pre><code>(m/domonad example-m [x1 [4 1] 
                        x2 (f1 x1) 
                        x3 (f2 x2) 
                        ] [x1 x2 x3]) ; =&gt; [4 5 10]
  </code></pre>
  
  <p>I was expecting <code>[[4 1] [5 :f1] [10 :f2]]</code></p>
  
  <p>Can I use the monad to solve this problem or do I need to fallback to a macro? </p>
  
  <p><em>EDIT</em>:</p>
  
  <p>I have analyzed the source of the problem in a bit more detail. The monad library expands the code above into:</p>
  
  <pre><code>(m-bind [4 1] 
          (fn [x1] 
            (m-bind (f1 x1) 
                    (fn [x2] 
                      (m-bind (f2 x2) 
                              (fn [x3] 
                                (m-result [x1 x2 x3])))))))
  </code></pre>
  
  <p>This shows that the variables <code>x1</code>, <code>x2</code>, <code>x3</code> cannot be monadic values but must be the 'real' values. Hence the behavior.</p>
  
  <p>A quick modification of m-bind </p>
  
  <pre><code>(defn m-bind   [[v k ] f]
                 [(f v) k])
  </code></pre>
  
  <p>yields <code>[[[[4 5 10] :f2] :f1] 1]</code>. I guess the solution is in the <code>m-result</code> function which has to unwrap this nested structure. </p>