title: Powerset - smalltalk
body: <p>could You explain why the following code is wrong:</p>
  
  <pre><code>powerset
       |ps|
       ps := Set with: (Set new).
       self do: [:item | ps addToPowerSet: item].
      ^ps
  
  addToPowerSet: elt
      | first second third var |
      first := self copy.  
      second := self copy.  
      third := Set new.
      second do:  [:item | var := item copy. var add: elt. third add: var.].
      ^ first addAll: third
  </code></pre>
  
  <p>Both methods are implemented in Set class.
  I have tried to debug it and I suppose that the mistake is here:</p>
  
  <pre><code>second do:  [:item | var := item copy. var add: elt. third add: var.].
  </code></pre>
  
  <p>But I have no idea what precisely.</p>
  
  <p>Ok, example to addToPowerSet method:</p>
  
  <p>Let's assumet that powerset variable ps is equal:</p>
  
  <pre><code>{{empty}, {a}, {b}, {a,b}}
  </code></pre>
  
  <p>and item = c.
  addToPowerSet method should return:</p>
  
  <pre><code>{
      {empty}, {a}, {b}, {a,b},
      {c}, {a, c}, {b, c}, {a,b,c}
  }
  </code></pre>