Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're using SWI-Prolog, you can achieve this for individual cases using <code>include/3</code>:</p> <pre><code>?- include(integer, [1,a,2,z,3,b,b], S). S = [1, 2, 3]. </code></pre> <p><code>include/3</code> calls the predicate <code>integer/1</code> on each element of the list and collects in S the elements X for which <code>call(integer, X)</code> is true (you could write this predicate yourself using <code>call/1</code> and the skeleton of the solution dasblinkenlight supplied).</p> <p>With this predicate in hand, we can easily write a predicate that lets us call <code>include/3</code> on the list multiple times, using different types each time:</p> <pre><code>select_type([], _, []). select_type([Type|Types], List, [S|Selected]) :- include(Type, List, S), select_type(Types, List, Selected). </code></pre> <p>This predicate takes a list of types as its first argument and gives a list of lists as its third argument. Each list in the latter contains the elements in <code>List</code> of the given type. Can be used thus:</p> <pre><code>?- select_type([integer, atom, var, float, is_list], [2.112,A,3.111,A,B,a,2,3,s,d], X). X = [[2, 3], [a, s, d], [A, A, B], [2.112, 3.111], []]. </code></pre> <p>Of course, this kind of predicate needn't be confined to built-in types, but will work with a list of any predicates which have room for an extra argument. E.g.,</p> <pre><code>?- select_type([plus(2,2)], [1,2,3,4,5,6], X). X = [[4]]. </code></pre> <p>because <code>call(plus(2,2), 4)</code> is true.</p> <hr> <p><strong>Edit:</strong></p> <p>To answer your question more directly, and in accordance with your clarification in the comments, you can write a predicate like your proposed <code>select/3</code> thus:</p> <pre><code>select_int_atom(List, Int, Atom) :- include(integer, List, Int), include(atom, List, Atom). </code></pre> <p>As you can see, this is equivalent to two consecutive calls to <code>include/3</code>. Note as remarked by mbratch, you don't want to use <code>select</code> for your predicate, since there is already a <code>select/3</code> in the core library.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload