I have 2 lists with random number of elemets. Eg A=[1,2,4,5] and B=[1,2,3]. Result should be 2. Code that I tried:
domains
Numbers1 = integer*
Numbers2 = integer*
int_list=integer*
predicates
nondeterm prinadl(integer, int_list)
clauses
//here going the code that read number that I've entered, and according to entered numer,programm should do something
answer(T):- T=5,
P = 0,
write ("Enter the 1st list"), readterm (int_list, L),
write ("Enter the 2nd list"), readterm (int_list, L2),
L2 = [H|V], prinadl(H, L), P1 = P + 1,
write(L2, P1, V).
prinadl (X, L):- L=[X|_], !.
prinadl (X, L):- L=[_|T], prinadl (X, T).
I'm totally new with prolog. Can you please say me where I'm wrong? All I need is to get number of matches printed to the console. Thanks in advance.
This answer is based on two things: first, guesswork. second,
if_/3
by @false.Let's define the meta-predicate
count_left_while2/4
.count_left_while2(P_2,Xs,Ys,N)
counts the numberN
of corresponding list items inXs
andYs
fulfillingP_2
. Proceeding from left to right,count_left_while2
stops at the first two items not satisfyingP_2
. It also stops when one list is empty, but the other one is not.Let's use it in combination with reified term equality predicate
(=)/3
, like this: