Given a query such as:
containsN(4,2,Z).
I should get: Z = [2,2,2,2].
or
containsN(4,W,[3,3,3,3])
I should get: W = 3.
So in other words, for the first example I need 4 instances of 2 in a list bound to Z.
For the second example I need the element in the list applied 4 times bound to W.
My attempt so far results in an infinite loop:
containsN(Y,W,Z) :- contains_helper(Y,W,Z).
contains_helper(0,_,_).
contains_helper(Y,W,[H|T]) :- W = H, Y0 is Y - 1, contains_helper(Y0,W,T).
The idea is, I call a helper function so that I can check the elements of Z one by one. When the counter reaches 0 I know the list is true because for each iteration H = W. However, I'm getting an infinite loop.