title: how print_parts() function work in composing program chapter2.3
body: <p>in book composing program <a href="http://composingprograms.com/pages/23-sequences.html" rel="nofollow">chapter 2.3</a>, there is a function print_part() makes me confused (whole code <a href="http://cs61a.org/assets/slides/11.py" rel="nofollow">here</a>):</p>
  
  <pre><code>&gt;&gt;&gt; def print_parts(tree, partition=[]):
          if is_leaf(tree):
              if root(tree):
                  print(' + '.join(partition))
          else:
              left, right = branches(tree)
              m = str(root(tree))
              print_parts(left, partition + [m])
              print_parts(right, partition)
  
  &gt;&gt;&gt; print_parts(partition_tree(6, 4))
  4 + 2
  4 + 1 + 1
  3 + 3
  3 + 2 + 1
  3 + 1 + 1 + 1
  2 + 2 + 2
  2 + 2 + 1 + 1
  2 + 1 + 1 + 1 + 1
  1 + 1 + 1 + 1 + 1 + 1
  </code></pre>
  
  <p>this function print all the ways using  parts up to 4 to partition 6. I've understand how the partition algorithm works in partition_tree() and have no problem to understand a partition tree:</p>
  
  <pre><code>                   4
         _________________________
         |                       |
         4                       3
     _______                    _____
     |     |                    |    |
     ..   ...                ...    ...
  </code></pre>
  
  <p>but I still don't know how to print partition ways from a partition tree. especially these lines:</p>
  
  <pre><code>print(' + '.join(partition))
  
  print_parts(left, partition + [m])
  print_parts(right, partition)
  # why call twice? and the recursion here didn't looks like the
  # way the print_parts() called in the beginning.
  </code></pre>
  
  <p>update:</p>
  
  <blockquote>
    <p>recursion here didn't looks like the way the print_parts() called in the beginning.</p>
  </blockquote>
  
  <p>take an simpler args as example to illustrate my confusion:</p>
  
  <pre><code>&gt;&gt;&gt; print_parts(partition_tree(3, 2))
  2 + 1
  1 + 1 + 1
  </code></pre>
  
  <p>the partition tree is:</p>
  
  <pre><code>                         2
             --------------------------------
            2                               1
      ----------------           ------------------------
     F               1           1                     F
                 ------        ------------
                T    F         1          F
                             -------
                            T      F
  </code></pre>
  
  <p>or</p>
  
  <pre><code>[2, [2, [False], [1, [True], [False]]], [1, [1, [1, [True], [False]],[False]],[False]]]
  </code></pre>
  
  <p>the above list is first pass into the func print_parts() as trees's value.</p>
  
  <p>when going to this line:</p>
  
  <pre><code>print_parts(left, partition + [m])
  </code></pre>
  
  <p>the left value is</p>
  
  <pre><code>[[2, [False], [1, [True], [False]]], [1, [1, [1, [True], [False]],[False]],[False]]]
  </code></pre>
  
  <p>it's no longer a tree because in the definition the tree should have a structure like: <code>[node, [branch],[branch]]</code>. if so, the recursion cannot works.</p>