Below is the code to remove a key (A) from a list if it is there. If it isn't there, it currently returns the entire list. I would like it instead to return 'false.' Sample outputs will be below as well.
mySelect(_, [], []).
mySelect(X, [Y|K], [Y|M]):- mySelect(X, K, M), (X \= Y).
mySelect(X, [X|K], R) :- mySelect(X, K, R).
Currently this will output:
?- my_delete(c,[a,b,c,d],R).
R = [a, b, d] .
?- my_delete(e,[a,b,c,d],R).
R = [a, b, c, d] .
I would like it to output:
?- my_delete(c,[a,b,c,d],R).
R = [a, b, d] .
?- my_delete(e,[a,b,c,d],R).
false .
Any pointers would be greatly appreciated!
CodePudding user response:
You can add another rule which first ensures that X is a member of L, before applying mySelect
.
delete(X, L, R) :- member(X, L), mySelect(X, L, R).
CodePudding user response:
Either partition the list into matches and non-matches:
filter( X , Ys, Zs ) :- filter(X,Ys,[_|_],Zs).
filter( _ , [] , [] , [] ) .
filter( X , [X|Ys] , [X|Xs] , Zs ) :- !, filter(X,Ys,Xs,Zs) .
filter( X , [Y|Ys] , Xs , [Y|Zs] ) :- filter(X,Ys,Xs,Zs) .
or count the number of items and test the count for being greater than zero:
filter( X , Ys, Zs ) :- filter(X,Ys,0,N,Zs), N > 0 .
filter( _ , [] , N , N , [] ) .
filter( X , [X|Ys] , T , N , Zs ) :- T1 is T 1, !, filter(X,Ys,T1,N, Zs) .
filter( X , [Y|Ys] , T , N , [Y|Zs] ) :- !, filter(X,Ys,T,N,Zs) .