1

Im learning Prolog and im trying to write a clause that check if one list (L1) is de result of duplicate other List(L2) N times,and return N as de result of checking how many times L2 is duplicated in L1. But I dont know how to compare the elements of two lists of different size and count how many times L2 is duplicated. Can you help me?.

false
  • 10,264
  • 13
  • 101
  • 209
Blanca
  • 11
  • 1

1 Answers1

0

One solution that comes to mind is using append/3. Since
append/3 is able to consume the beginning prefix of a list:

?- append([1,2],X,[1,2,3,5]).
X = [3, 5].

So a quite straight forwards solution does the following,
assuming the smaller list L2 and the bigger list L1 are given:

times(_, 0, []).
times(L2, M, L1) :- append(L2, H, L1), times(L2, N, H), M is N+1.

Seems to work:

?- times([a,b,c],X,[a,b,c,a,b,c]).
X = 2 ;
false.

?- times([a,b,c],X,[a,b,c,a,b,d]).
false.
Rúben Dias
  • 336
  • 2
  • 9