title: Prolog: lists consisting of one element
body: <p>I believe that the problem I have is very silly, but couldn't find an answer searching online. I want a function to always return a list of elements, even in the case the list consists of just one element. I don't know if it is always like that in prolog (i.e. that one element lists are transformed in a simple constant) but here I go into detail.</p>
  
  <p>First off, I have a set of predicates (like <code>composeBase(&lt;,&lt;,[&lt;]).</code>) based on which the function I was talking about, <code>compose/3</code>, makes its computations:</p>
  
  <pre><code>compose(X, Y, Z):-
      compositionBase(X,Y,Z).
  </code></pre>
  
  <p>So, for instance, <code>compose/3</code> does this:</p>
  
  <pre><code>compose(&lt;, &lt;, L).
  L = (&lt;) .
  compose(&lt;, =, L).
  L = (&lt;) .
  compose(=, =, L).
  L = (=) .
  </code></pre>
  
  <p>So, why in the first example does it not return <code>[&lt;]</code> if the predicate says <code>composeBase(&lt;,&lt;,[&lt;]).</code>? Is there a way to make it return the list <code>[&lt;]</code> instead? </p>
  
  <p>If there is not or my question doesn't make any sense, here is where the problem comes with not being able to do so (if there is, don't even bother to go on reading of course! :) Unless you feel like giving me suggestions for my naive code, which I would very much appreciate as a newbie).</p>
  
  <p>I have to write a function <code>composeList/3</code> which, given two lists of elements, should compute a list containing all the possible compositions of the elements in the two set.</p>
  
  <p>Therefore, for example, <code>composeList/3</code> should do this:</p>
  
  <pre><code>composeList([&lt;], [&lt;, =], L).
  L = [&lt;, =] .
  composeList([&lt;,=], [&lt;, =], L).
  L = [&lt;, =] .
  </code></pre>
  
  <p>And here's the code:</p>
  
  <pre><code>composeList(_,[],[]).
  composeList([],_,[]).
  composeList([X| Xs], [Y| Ys], L):-
      compose(X,Y,L1),
      composeList(Xs, [Y| Ys], L2),
      composeList([X| Xs], Ys, L3),
      union(L1, L2, L4),
      union(L3,L4,L).
  </code></pre>
  
  <p>So, as confirmed by doing <code>trace.</code>, I believe the problem occurs when <code>compose/3</code> returns a simple constant and not a list. For instance, in the example above it returns <code>false .</code> because, since L1 is set <code>&lt;</code>, the first union fails. How can I fix this in a casual way?</p>
  
  <p>By the way, here is what <code>trace.</code> does:</p>
  
  <pre><code> [trace] 123 ?- compositionListe([&lt;],[&lt;,=],L).
  Call: (7) compositionListe([&lt;], [&lt;, =], _G8032) ? creep
  Call: (8) compose(&lt;, &lt;, _G8122) ? creep
  Call: (9) compositionBase(&lt;, &lt;, _G8122) ? creep
  Exit: (9) compositionBase(&lt;, &lt;, [&lt;]) ? creep
  Exit: (8) compose(&lt;, &lt;, [&lt;]) ? creep
  Call: (8) compositionListe([], [&lt;, =], _G8128) ? creep
  Exit: (8) compositionListe([], [&lt;, =], []) ? creep
  Call: (8) compositionListe([&lt;], [=], _G8131) ? creep
  Call: (9) compose(&lt;, =, _G8131) ? creep
  Exit: (9) compose(&lt;, =, &lt;) ? creep
  Call: (9) compositionListe([], [=], _G8134) ? creep
  Exit: (9) compositionListe([], [=], []) ? creep
  Call: (9) compositionListe([&lt;], [], _G8137) ? creep
  Exit: (9) compositionListe([&lt;], [], []) ? creep
  Call: (9) lists:union(&lt;, [], _G8137) ? creep
  Fail: (9) lists:union(&lt;, [], _G8137) ? creep
  </code></pre>
  
  <p>Thank you very much in advance!</p>