To the existing answers, I would like to add an answer that is pretty general in the sense that you can use it in several ways.
Building block: list_element_number/3
I start with the following predicate, defining the relationship between:
:
list_element_number(Ls0, E, N) :-
tfilter(=(E), Ls0, Ls),
length(Ls, N).
tfilter/3 library(reif). count, . , , Haskell , :
?- list_element_number([a,b,c], a, N).
N = 1.
, , :
?- list_element_number([a,b,c], X, 1).
X = a ;
X = b ;
X = c ;
false.
:
?- list_element_number([a,b,E], X, 2).
E = X, X = a ;
E = X, X = b ;
false.
:
?- list_element_number([A,B,C], X, 3).
A = B, B = C, C = X ;
false.
, :
?- list_element_number(Ls, E, N).
Ls = [],
N = 0 ;
Ls = [E],
N = 1 ;
Ls = [E, E],
N = 2 ;
Ls = [E, E, E],
N = 3 .
:
?- length(Ls, _), list_element_number(Ls, E, N).
Ls = [],
N = 0 ;
Ls = [E],
N = 1 ;
Ls = [_160],
N = 0,
dif(E, _160) ;
Ls = [E, E],
N = 2 .
: list_singletons/2
, list_singletons/2 :
list_singletons(Ls, Singles) :-
tfilter(count_one(Ls), Ls, Singles).
count_one(Ls, E, T) :-
list_element_number(Ls, E, Num),
cond_t(Num=1, true, T).
cond_t/3 () tfilter/3 library(reif).
. -, , :
?- list_singletons([1,1,2,2,3,4,4,5], Singles).
Singles = [3, 5].
.
:
?- list_singletons([A,B], Singles).
A = B,
Singles = [] ;
Singles = [A, B],
dif(A, B).
: A = B, , . A B, A, B .
:
?- list_singletons([A,A], Singles).
Singles = [].
, :
?- length(Ls, _), list_singletons(Ls, Singles).
Ls = Singles, Singles = [] ;
Ls = Singles, Singles = [_7216] ;
Ls = [_7216, _7216],
Singles = [] ;
Ls = Singles, Singles = [_7828, _7834],
dif(_7828, _7834) ;
Ls = [_7216, _7216, _7216],
Singles = [] ;
Ls = [_7910, _7910, _7922],
Singles = [_7922],
dif(_7910, _7922) .
, logical-purity.