Reputation: 39
I'm learning prolog but I have a problem with a code. I want to check if an element exists in a list of lists and if it exists I want to get the list that contains that element to manipulate the list. I made a clause to prove that the element is or not in the list of lists but I don't know how to get that list, please help me.
memberlist(X,[H|_]) :-
member(X,H).
memberlist(X,[_|T2]) :-
memberlist(X,T2).
I do this query:
memberlist("product1",[["product2", 100, "available"],["product3", 100, "sold out"],["product1", 200, "available"]]).
and I get: true
but also I want to get the list that contains the string "product1"
so the result that I'm looking for is: ["product1", 200, "available"]
Upvotes: 0
Views: 159
Reputation: 211
How about introducing a third argument to store the matching list? e.g.:
memberlist(X,[H|_],H) :-
member(X,H).
memberlist(X,[_|T2],Y) :-
memberlist(X,T2,Y).
Example query:
memberlist("product1", [["product2", 100, "available"],["product3", 100, "sold out"],["product1", 200, "available"]], Result).
Produces the outcome:
Result = ["product1", 200, "available"]
Upvotes: 1