title: Recursion query that checks if X exists in a Prolog database
body: <p>I'm trying to create a query <code>exists(X)</code> that returns true if X exists in a prolog database.</p>
  
  <p>Prolog Database</p>
  
  <pre><code>store(best_smoothies, [alan,john,mary],
        [ smoothie(berry, [orange, blueberry, strawberry], 2),
          smoothie(tropical, [orange, banana, mango, guava], 3),
          smoothie(blue, [banana, blueberry], 3) ]).
  store(all_smoothies, [keith,mary],
        [ smoothie(pinacolada, [orange, pineapple, coconut], 2),
          smoothie(green, [orange, banana, kiwi], 5),
          smoothie(purple, [orange, blueberry, strawberry], 2),
          smoothie(smooth, [orange, banana, mango],1) ]).
  store(smoothies_galore, [heath,john,michelle],
        [ smoothie(combo1, [strawberry, orange, banana], 2),
          smoothie(combo2, [banana, orange], 5),
          smoothie(combo3, [orange, peach, banana], 2),
          smoothie(combo4, [guava, mango, papaya, orange],1),
          smoothie(combo5, [grapefruit, banana, pear],1) ]).
  </code></pre>
  
  <p>My attempt</p>
  
  <pre><code>exists(X) :- store(_,_,S), isIn(X,S).
  
  isIn(X, [smoothie(X,_,_)|[]]).
  isIn(X, [smoothie(N,_,_)|T]) :- isIn(X,T).
  </code></pre>
  
  <p>Surprisingly it only returns true for <code>blue</code>, <code>smooth</code> and <code>combo5</code>. Is there something wrong with my recursion? (I don't think so since it was able to read <code>blue</code>, <code>smooth</code> and all the way down to <code>combo5</code>)</p>
  
  <p>Output that I'm getting</p>
  
  <pre><code>?- exists(combo1).
  false
  
  ?- exists(X).
  X = blue;
  X = smooth;
  X = combo5;
  false
  </code></pre>
  
  <p>Appreciate any help on this. Many thanks!</p>
  
  <p><strong>EDIT: Revised Attempt</strong></p>
  
  <pre><code>exists(X) :- store(_,_,S), isIn(X,S).
  
  isIn(X, [smoothie(X,_,_)]).
  isIn(X, [smoothie(N,_,_)|T]) :- isIn(X,T).
  </code></pre>
  
  <p><strong>EDIT: Fixed</strong></p>
  
  <pre><code>exists(X) :- store(_,_,S), isIn(X,S).
  
  isIn(X, [smoothie(X,_,_)|_]).
  isIn(X, [smoothie(N,_,_)|T]) :- isIn(X,T).
  </code></pre>