Reputation: 23
I have a list of pairs (represent some locations in a matrix) and I like to filter the list to contain only locations at raw i. this is what I wrote:
getRaw(_,[],[]).
getRaw(i,[(i,j)|LocationsTail],[(i,j)|result]) :-
getRaw(i,LocationsTail,result).
getRaw(i,[(k,t)|LocationsTail],result) :-
i\=k,
getRaw(i,LocationsTail,result).
This is what I wrote at the console:
1 ?- getRaw(1,[(1,2)],R).
false.
Whats wrong with it?
Upvotes: 1
Views: 159
Reputation: 23
the problem was the lower case of the variables. here is the fixed code:
getRaw(_,[],[]).
getRaw(Raw,[(Raw,Col)|LocationsTail],[(Raw,Col)|Result]) :-
getRaw(Raw,LocationsTail,Result).
getRaw(Raw,[(K,_)|LocationsTail],Result) :-
Raw\=K,
getRaw(Raw,LocationsTail,Result).
Upvotes: 0
Reputation: 10152
What's wrong: Variables start with a capital letter, you are using an incorrect version of negation. Here is a clean approach:
First, define the precise condition you are after in a separate predicate:
fst_pair(X,(X,_)).
Then, try to define a more general predicate, that has its truth value as a separate argument:
fst_pair_truth(X,(X,_),true).
fst_pair_truth(X,(Y,_),false) :-
dif(X,Y).
Note that the negative case is not simply the negation of the positive case: Both expect pairs.
Now, your definition is, maybe reconsider the name...
getRaw(I, Xs, Ys) :-
tfilter(fst_pair_truth(I), Xs, Ys).
See this answer for a definition of tfilter/3
.
Upvotes: 1