title: GISC exercise 11.22d: Why is this stateful?
body: <p>Exercise 11.22d of David Touretzky's &quot;A Gentle Introduction to Symbolic Computation&quot; states:</p>
  <p>&quot;[...] Write a function <code>COUNT-BASES</code> that
  counts the number of bases of each type in a DNA strand, and returns
  the result as a table. Your function should work for both single-and
  double-stranded DNA. Example: <code>(COUNT-BASES '((G C) (A T) (T A) (T A) (C G)))</code> should return <code>((A 3) (T 3) (G 2) (C 2))</code>, whereas
  <code>(COUNT-BASES '(A G T A C T C T))</code> should return <code>((A 2) (T 3) (G 1) (C 2))</code>. [...]&quot;</p>
  <p>My solution:</p>
  <pre><code>(defun count-bases (strand)
    (labels ((flatten (tree)
             (cond
               ((atom tree) (list tree))
               (t (append (flatten (car tree))
                          (and (cdr tree) 
                               (flatten (cdr tree))))))))
  
      (let ((flat-strand (flatten strand))
            (cnt-list '((a 0) (t 0) (c 0) (g 0))))
        (dolist (el flat-strand cnt-list) 
          (incf (cadr (assoc el cnt-list)))))))
  </code></pre>
  <p>So the idea is to first flatten <code>STRAND</code> and then count with <code>assoc</code> and <code>incf</code>.</p>
  <p>The problem is that this somehow retains the state of <code>cnt-list</code>, like so:</p>
  <pre><code>(count-bases '((g c) (a t) (t a) (t a) (c g))) ;; ((A 3) (T 3) (C 2) (G 2))
  (count-bases '((g c) (a t) (t a) (t a) (c g))) ;; ((A 6) (T 6) (C 4) (G 4))
  ;; etc
  </code></pre>
  <p>I am aware that incf is de/con-structive, nonetheless cnt-list should reset after the function returns, right?</p>