Rubens already informed you of your mistake. I'll just add a note to the style: often in Prolog, he preferred to directly encode the template in the chapter arguments:
occurences(_, [], []). occurences(X, [X|T], [X|TMP]) :- occurences(X,T,TMP), !. occurences(X, [_|T], Res) :- occurences(X,T,Res).
I adjusted the second sentence of 'output' from [X,TMP] to [X|TMP] and note the reduction: without it, the procedure produces more results than required:
?- occurences(a,[a,b,c,a],Res). Res = [a, a] ; Res = [a] ; Res = [a] ; Res = [] ; false.
with a cut:
?- occurences(a,[a,b,c,a],Res). Res = [a, a].
edit @false messed up the nasty bug: here's the fix using the if / then / else construct
occurences(_, [], []). occurences(X, [Y|T], Os) :- ( X = Y -> Os = [X|R] ; Os = R ), occurences(X,T,R).