The solution works because the first argument is the main list. In some other cases this is not true:
?- count([E], a, 0).
false.
Here we ask
How should an element of a Elist of length 1 look so that the list contains 0 entries a?
And actually there are answers to this, for example, E = bor E = c:
?- count([b],a,0).
true.
?- count([c],a,0).
true.
. , . ?
count([], _, 0).
count([E|Es], F, N0) :-
count(Es, F, N1),
if_(E = F, D = 1, D = 0),
N0 is N1+D.
if_/3 (=)/3.
?- length(Xs, I), count_dif(Xs, a, N).
Xs = [],
I = N, N = 0
; Xs = [a],
I = N, N = 1
; Xs = [_A],
I = 1,
N = 0,
dif(_A, a)
; Xs = [a, a],
I = N, N = 2
; Xs = [_A, a],
I = 2,
N = 1,
dif(_A, a) ;
Xs = [a, _A],
I = 2,
N = 1,
dif(_A, a) ;
Xs = [_A, _B],
I = 2,
N = 0,
dif(_A, a),
dif(_B, a)
...
, library(clpfd), SICStus, YAP SWI.
:- use_module(library(clpfd)).
count([], _, 0).
count([E|Es], F, N0) :-
N0
if_(E = F, D = 1, D = 0),
N0
count(Es, F, N1).
:
?- count([a,a|_], a, 1).
false.
?- N
false.