title: Implementing a simple version of Prolog findall without using the built-in findall
body: <p>I am trying to implement a simple version of findall in Prolog without using the built-in findall or similar built-in predicates -- just as a learning exercise.</p>
  <p>For example, suppose there is a relation a/1 in my database,</p>
  <pre><code>a(1).
  a(11).
  a(13).
  </code></pre>
  <p>and I want a relation like find_the_as(List) to give me List = [1,11,13].</p>
  <p>I can do this with a &quot;fail&quot; based loop using assert and retract, but I am thinking there must be a recursive way to do this using an accumulator (and I am just not seeing it).</p>
  <p>Thanks in advance!</p>
  <p>for example:</p>
  <p>a(1).<br />
  a(11).<br />
  a(13).</p>
  <pre><code>%
  % collect all the N such that a(N) 
  % into a List
  %  
  collect_n_such_that_a_of_n(List) :- 
     retractall(the_n_such_that_a_of_n(_)),
     assert(the_n_such_that_a_of_n([])),
     (
        a(N),
        the_n_such_that_a_of_n(As),
        retractall(the_n_such_that_a_of_n(_)),
        assert(the_n_such_that_a_of_n([N|As])),
        fail
     ) ; true,
     the_n_such_that_a_of_n(List).
     
  </code></pre>