Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A variable match a <strong>term</strong>, and the anonimus variable is not exception. A list is just syntax sugar for a binary relation, between head and tail. So a variable can match the list, the head, or the tail, but not an unspecified sequence.</p> <p>Some note I hope will help you:</p> <blockquote> <p>listcount(L, N) :- listcountA(LS, [], N).</p> </blockquote> <p>In Prolog, predicates are identified by name <strong>and</strong> num.of.arguments, so called <strong>functor</strong> and <strong>arity</strong>. So usually 'service' predicates with added arguments keep the same name.</p> <blockquote> <p>listcountA([X|Tail], [? [X, B], ?], N) :- B is B+1, listcountA(Tail, [? [X,B] ?], N).</p> </blockquote> <p>B is B+1 will never succeed, you <strong>must</strong> use a new variable. And there is no way to match inside a list, using a 'wildcard', as you seem to do. Instead write a predicate to find and update the counter.</p> <p>A final note: usually <strong>pairs</strong> of elements are denoted using a binary relation, conveniently some (arbitrary) operator. For instance, most used is the dash.</p> <p>So I would write </p> <pre><code>listcount(L, Counters) :- listcount(L, [], Counters). listcount([X | Tail], Counted, Counters) :- update(X, Counted, Updated), !, listcount(Tail, Updated, Counters). listcount([], Counters, Counters). update(X, [X - C | R], [X - S | R]) :- S is C + 1. update(X, [H | T], [H | R]) :- update(X, T, R). update(X, [], [X - 1]). % X just inserted </code></pre> <p>update/3 can be simplified using some library predicate, 'moving inside' the recursion. For instance, using select/3:</p> <pre><code>listcount([X | Tail], Counted, Counters) :- ( select(X - C, Counted, Without) -&gt; S is C + 1 ; S = 1, Without = Counted ), listcount(Tail, [X - S | Without], Counters). listcount([], Counters, Counters). </code></pre>
 

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