title: Prolog - extract numbers from nested list
body: <p>I trying to write a function <code>getNumbers(List,Result)</code> such that <code>List</code> is a list which his elements can be integer or list of lists , for example  - </p>
  
  <pre><code>List = [1,[1,2,[3],[4]],2,[4,5]]
  List = [1,[1,1,[1],[1]],1,[1,1,[[[[[1]]]]]]]
  List = [[4,[[]],2],[[1],[],[1]]]
  etc..
  </code></pre>
  
  <p>And the output should be all the numbers stored in that <code>List</code> , for example - </p>
  
  <pre><code>?- getNumbers([1,[1,2,[3],[4]],2,[4,5]],R).
  R = [1,2,3,4,5].
  ?- getNumbers([1,[1,1,[1],[1]],1,[1,1,[[[[[1]]]]]]],R).
  R = [1].
  ?- getNumbers([],R).
  R = [].
  ?- getNumbers([[4,[[]],2],[[1],[],[1]]],R).
  R = [1,2,4].
  </code></pre>
  
  <p>So far I tried the follow code -</p>
  
  <pre><code>getNumbers([],Result) :- Result=[],!.
  getNumbers([H|Rest],Result) :- getNumbers(Rest,NewResultRest),
      ( atomic(H) -&gt;  
        Result = [H|NewResultRest]    
      ; getNumbers(H,NewResultHead),Result = [NewResultHead|NewResultRest]    ).
  </code></pre>
  
  <p>But it gives wrong result , like -</p>
  
  <pre><code> getNumbers([[2],5,7,[3,6,5]],Result).
  Result = [[2], 5, 7, [3, 6, 5]].
  </code></pre>
  
  <p>It seems that the function doesn't exludes <code>2</code> from <code>[2]</code> or any other numbers stored in nested list.</p>
  
  <p>How could I fix my implementation ?</p>