title: Accumulators, conj and recursion
body: <p>I've solved 45 problems from 4clojure.com and I noticed a recurring problem in the way I try to solve some problems using recursion and accumulators.</p>
  
  <p>I'll try to explain the best I can what I'm doing to end up with fugly solutions hoping that some Clojurers would "get" what I'm not getting.</p>
  
  <p>For example, problem 34 asks to write a function (without using <em>range</em>) taking two integers as arguments and creates a range (without using range).  Simply put you do (... 1 7) and you get (1 2 3 4 5 6).</p>
  
  <p>Now this question is not about solving this particular problem.</p>
  
  <p>What if I <em>want</em> to solve this using recursion and an accumulator?</p>
  
  <p>My thought process goes like this:</p>
  
  <ul>
  <li><p>I need to write a function taking two arguments, I start with <em>(fn [x y] )</em></p></li>
  <li><p>I'll need to recurse and I'll need to keep track of a list, I'll use an accumulator, so I write a 2nd function inside the first one taking an additional argument:</p>
  
  <p>(fn
   [x y]<br>
   ((fn g [x y acc] ...)
    x
    y
    '())</p></li>
  </ul>
  
  <p><em>(apparently I can't properly format that Clojure code on SO!?)</em></p>
  
  <p>Here I'm already not sure I'm doing it correctly: the first function <em>must</em> take exactly two integer arguments (not my call) and I'm not sure: if I want to use an accumulator, can I use an accumulator without creating a nested function?</p>
  
  <p>Then I want to <em>conj</em>, but I cannot do:</p>
  
  <pre><code>(conj 0 1)
  </code></pre>
  
  <p>so I do weird things to make sure I've got a sequence first and I end up with this:</p>
  
  <pre><code>(fn
     [x y]
     ((fn g [x y acc] (if (= x y) y (conj (conj acc (g (inc x) y acc)) x)))
      x
      y
      '()))
  </code></pre>
  
  <p>But then this produce this:</p>
  
  <pre><code>(1 (2 (3 4)))
  </code></pre>
  
  <p>Instead of this:</p>
  
  <pre><code>(1 2 3 4)
  </code></pre>
  
  <p>So I end up doing an additional <em>flatten</em> and it works but it is totally ugly.</p>
  
  <p>I'm beginning to understand a few things and I'm even starting, in some cases, to "think" in a more clojuresque way but I've got a problem writing the solution.</p>
  
  <p>For example here I decided:</p>
  
  <ul>
  <li>to use an accumulator</li>
  <li>to recurse by incrementing <em>x</em> until it reaches <em>y</em></li>
  </ul>
  
  <p>But I end up with the monstrosity above.</p>
  
  <p>There are a <em>lot</em> of way to solve this problem and, once again, it's not what I'm after.</p>
  
  <p>What I'm after is how, after I decided to cons/conj, use an accumulator, and recurse, I can end up with this (not written by me):</p>
  
  <pre><code>#(loop [i %1
          acc nil]
    (if (&lt;= %2 i)
      (reverse acc)
      (recur (inc i) (cons i acc))))
  </code></pre>
  
  <p>Instead of this:</p>
  
  <pre><code>((fn
    f
    [x y]
    (flatten
     ((fn
       g
       [x y acc]
       (if (= x y) acc (conj (conj acc (g (inc x) y acc)) x)))
      x
      y
      '())))
   1
   4)
  </code></pre>
  
  <p>I take it's a start to be able to solve a few problems but I'm a bit disappointed by the ugly solutions I tend to produce...</p>