title: Prolog: fill list with n elements
body: <p>Need to make a predicate, fill(L,X,N), where L is a list formed containing N elements X. If N &lt;= 0 or N != length of L, L should be an empty list.</p>
  
  <p>Here's what I've done, I've never been able to get the if, else.. structure working correctly in Prolog:</p>
  
  <pre><code>fill(L,X,N) :-
     ((N =&lt; 0) -&gt; 
     L = [];
     length(L,I),
     ((N =\+= I) -&gt; 
     L = [];
     fill2(L,X,N))).
  fill2([H|T],X,N2) :-
     NewN = N2 - 1,
     H = X,
     fill2(T,X,NewN).
  </code></pre>
  
  <p>I also have a simpler version, that works except when N != length of L</p>
  
  <pre><code>fill(L,_,N) :-
     N =&lt; 0,
     L = [].
  fill([H|T],X,N) :-
     NewN = N - 1,
     H = X,
     fill(T,X,NewN).
  </code></pre>
  
  <p>So, for example, fill(L,20,4) returns L = [20,20,20,20], but fill([20,20,20],X,2) doesn't return L = [].</p>