title: Transitive closure from a list using Haskell
body: <p>I need to produce transitive closure on list using Haskell.</p>
  
  <p><strong>So far I got this:</strong></p>
  
  <pre><code>import Data.List
  qq [] = []
  qq [x] = [x]
  qq x = vv (sort x)
  
  vv (x:xs) = [x] ++ (member [x] [xs]) ++  (qq xs)
  
  member x [y] = [(x1, y2) | (x1, x2) &lt;- x, (y1, y2) &lt;- qq (y), x2 == y1]
  </code></pre>
  
  <p><strong>Output 1:</strong></p>
  
  <pre><code>*Main&gt; qq [(1,2),(2,3),(3,4)]
  [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]
  </code></pre>
  
  <p><strong>Output 2:</strong></p>
  
  <pre><code>*Main&gt; qq [(1,2),(2,3),(3,1)]
  [(1,2),(1,3),(1,1),(2,3),(2,1),(3,1)]
  </code></pre>
  
  <p>The problem is with second output. Instead of checking for additional transitive closure on the new produced list, it just returns result.</p>
  
  <p>To prototype haskell code I used this <strong>Python code</strong>:</p>
  
  <pre><code>def transitive_closure(angel):
      closure = set(angel)
      while True:
          new_relations = set((x,w) for x,y in closure for q,w in closure if q == y)
          closure_until_now = closure | new_relations    
          if closure_until_now == closure:
              break    
          closure = closure_until_now    
      return closure
  
  print transitive_closure([(1,2),(2,3),(3,1)])
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>set([(1, 2), (3, 2), (1, 3), (3, 3), (3, 1), (2, 1), (2, 3), (2, 2), (1, 1)])
  </code></pre>
  
  <p>This is right output that I need in my Haskell function.</p>
  
  <p>How to do the same thing in my Haskell code? (I need to recreate <code>if</code> statement from Python code to the Haskell code)</p>