title: Extract Facts as Matrix in Prolog
body: <p>Suppose we have the following:</p>
  
  <pre><code>edge(a, 1, 10).
  edge(b, 2, 20).
  edge(c, 3, 30).
  edge(d, 4, 40).
  </code></pre>
  
  <p>I want to extract a matrix representation (<code>M</code>) of these facts, such that</p>
  
  <pre><code>M = [[a,b,c,d],[1,2,3,4],[10,20,30,40]]
  </code></pre>
  
  <p>Here's a no-brainer solution:</p>
  
  <pre><code>edgeMatrix(M) :-
    findall(A, edge(A, _, _), As),
    findall(B, edge(_, B, _), Bs),
    findall(C, edge(_, _, C), Cs),
    M = [As, Bs, Cs].
  </code></pre>
  
  <p>There are some problems to this approach, however, <em>viz</em>:</p>
  
  <ol>
  <li>we traverse the database <em>n</em> times, where <em>n</em> is the number of arguments; and</li>
  <li>this doesn't generalize very well to an arbitrary <em>n</em>.</li>
  </ol>
  
  <p>So the question is: what is the most idiomatic way to achieve this in Prolog?</p>