Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, to sum it up, you're doing it wrong.</p> <p>In Prolog, when we want to express a relation instead of a function (several results possible instead of one), we don't use <code>findall/3</code> and <code>member/2</code> directly. We rather state what the relation is and then maybe once it's done if we need a list of results we use <code>findall/3</code>.</p> <p>Here what it means is that we want to express the following relation:</p> <blockquote> <p>Take a list of <code>Key-Values</code> and return a list of <code>Key-[Value]</code> where <code>Value</code> is a member of the <code>Values</code> list.</p> </blockquote> <p>We could do so as follows:</p> <pre><code>% The base case: handle the empty list a_pair_list([], []). % The case where the Values list is empty, then the resulting [Value] is [] a_pair_list([Key-[]|List], [Key-[]|Result]) :- a_pair_list(List, Result). % The case where the Values list is not empty, then Value is a member of Values. a_pair_list([Key-[Not|Empty]|List], [Key-[Value]|Result]) :- member(Value, [Not|Empty]), a_pair_list(List, Result). </code></pre> <p>Once this relation is expressed, we can already obtain all the info we wish:</p> <pre><code>?- a_pair_list([1-[a, b], 2-[], 3-[c]], Result). Result = [1-[a], 2-[], 3-[c]] ; Result = [1-[b], 2-[], 3-[c]] ; false. </code></pre> <p>The desired list is now just a fairly straight-forward <code>findall/3</code> call away:</p> <pre><code>all_pairs_lists(Input, Output) :- findall(Result, a_pair_list(Input, Result), Output). </code></pre> <p>The important thing to remember is that it's way better to stay away from extra logical stuff: <code>!/0</code>, <code>findall/3</code>, etc... because it's often leading to less general programs and/or less correct ones. Here since we can express the relation stated above in a pure and clean way, we should. This way we can limit the annoying use of <code>findall/3</code> at the strict minimum.</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.
    3. 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