I'm learning Prolog right now and the recursive thinking is difficult to me.
I have two lists e.g. L1=[1,2,3]
and L2=[3,1,2]
and I should check if all elements of L1
are contained in L2
So allMembers(L1,L2).
should return true
and allMembers(L1,[2,3,4]).
should return false
, because there is no 4
in L1
I thought it would be possible to get the head of the one list and return true if it finds that element in the other list (by splitting it down until the list only contains one item), but I have no idea how to do that in Prolog.
It would be nice if you could help me getting faster on the uptake (?)
I found similar problems here, but couldn't find an answer to my specific problem :/
Edit:
I now have:
isMemberOf(X,[X|_]).
isMemberOf(X,[_|T]):- member(X,T).
which I need to use for any element in L1
to L2
Quick solution using de facto standard predicates:
all_from_first_in_second(List1, List2) :-
forall(member(Element,List1), member(Element,List2)).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With