title: Do you find you still need variables you can change, and if so why?
body: <p>One of the arguments I've heard against functional languages is that single assignment coding is too hard, or at least significantly harder than "normal" programming.</p>
  
  <p>But looking through my code, I realized that I really don't have many (any?) use patterns that can't be written just as well using single assignment form if you're writing in a reasonably modern language.</p>
  
  <p>So what are the use cases for variables that vary within a single invocation of their scope?  Bearing in mind that loop indexes, parameters, and other scope bound values that vary <em>between</em> invocations aren't multiple assignments in this case (unless you <em>have to</em> change them in the body for some reason), and assuming that you are writing in something a far enough above the assembly language level, where you can write things like</p>
  
  <pre><code>values.sum
  </code></pre>
  
  <p>or (in case sum isn't provided)</p>
  
  <pre><code>function collection.sum --&gt; inject(zero, function (v,t) --&gt; t+v )
  </code></pre>
  
  <p>and</p>
  
  <pre><code>x = if a &gt; b then a else b
  </code></pre>
  
  <p>or </p>
  
  <pre><code>n = case s 
    /^\d*$/ : s.to_int
    ''      : 0
    '*'     : a.length
    '?'     : a.length.random
    else    fail "I don't know how many you want"
  </code></pre>
  
  <p>when you need to, and have list comprehensions, map/collect, and so forth available.</p>
  
  <p>Do you find that you still want/need mutable variables in such an environment, and if so, what for?</p>
  
  <p>To clarify, I'm not asking for a recitation of the objections to SSA form, but rather concrete examples where those objections would apply.  I'm looking for bits of code that are clear and concise with mutable variables and couldn't be written so without them.</p>
  
  <p>My favorite examples so far (and the best objection I expect to them):</p>
  
  <ol>
  <li><p>Paul Johnson's <a href="https://stackoverflow.com/questions/610956/do-you-find-you-still-need-variable-you-can-change-and-if-so-why/622128#622128">Fisher-Yates algorithm</a> answer, which is pretty strong when you include the big-O constraints.  But then, as catulahoops points out, the big-O issue isn't tied to the SSA question, but rather to having mutable data types, and with that set aside the algorithm can be written rather clearly in SSA:</p>
  
  <pre><code> shuffle(Lst) -&gt;
       array:to_list(shuffle(array:from_list(Lst), erlang:length(Lst) - 1)).
   shuffle(Array, 0) -&gt; Array;
   shuffle(Array, N) -&gt;
       K = random:uniform(N) - 1,
       Ek = array:get(K, Array),
       En = array:get(N, Array),
       shuffle(array:set(K, En, array:set(N, Ek, Array)), N-1).
  </code></pre></li>
  <li><p>jpalecek's <a href="https://stackoverflow.com/questions/610956/do-you-find-you-still-need-variable-you-can-change-and-if-so-why/622057#622057">area of a polygon</a> example:</p>
  
  <pre><code>def area(figure : List[Point]) : Float = {
    if(figure.empty) return 0
    val last = figure(0)
    var first= figure(0)
    val ret = 0
    for (pt &lt;- figure) {
      ret+=crossprod(last - first, pt - first)
      last = pt
    }
    ret
  }
  </code></pre>
  
  <p>which might still be written something like:</p>
  
  <pre><code>def area(figure : List[Point]) : Float = {
      if figure.length &lt; 3
          0
        else
          var a = figure(0)
          var b = figure(1)
          var c = figure(2)
          if figure.length == 3
              magnitude(crossproduct(b-a,c-a))
            else 
              foldLeft((0,a,b))(figure.rest)) { 
                 ((t,a,b),c) =&gt; (t+area([a,b,c]),a,c)
                 }
  </code></pre>
  
  <p>Or, since some people object to the density of this formulation, it could be recast:</p>
  
  <pre><code>def area([])    = 0.0   # An empty figure has no area
  def area([_])   = 0.0   # ...nor does a point
  def area([_,_]) = 0.0   # ...or a line segment
  def area([a,b,c]) =     # The area of a triangle can be found directly
      magnitude(crossproduct(b-a,c-a))
  def area(figure) =      # For larger figures, reduce to triangles and sum
      as_triangles(figure).collect(area).sum
  
  def as_triangles([])      = []  # No triangles without at least three points
  def as_triangles([_])     = []
  def as_triangles([_,_])   = []
  def as_triangles([a,b,c | rest) = [[a,b,c] | as_triangles([a,c | rest])]
  </code></pre></li>
  <li><p>Princess's point about the difficulty of implementing O(1) queues with immutable structures is interesting (and may well provide the basis for a compelling example) but as stated it's fundamentally about the mutability of the data structure, and not directly about the multiple assignment issue.</p></li>
  <li><p>I'm intrigued by the Sieve of Eratosthenes answer, but unconvinced.  The proper big-O, pull as many primes as you'd like generator given in the paper he cited does not look easy to implement correctly with or without SSA.</p></li>
  </ol>
  
  <hr>
  
  <p>Well, thanks everyone for trying.  As most of the answers turned out to be either 1) based on mutable data structures, not on single-assignment, and 2) to the extent they were about single assignment form easily countered by practitioners skilled in the art, I'm going to strike the line from my talk and / or restructure (maybe have it in backup as a discussion topic in the unlikely event I run out of words before I run out of time). </p>
  
  <p>Thanks again.</p>