I'm new to Prolog and I got stuck on writing this predicate. Basically I'm given a list and I need to find the most common sublist of size N. Examples:
most_common_sublist([1,2,2,3,2,2,4,2,2,3],1,L)
, output should beL=[2]
;most_common_sublist([1,2,2,3,2,2,4,2,2,3],2,L)
, output should beL=[2,2]
;most_common_sublist([1,2,2,3,2,2,4,2,2,3],3,L)
, output should beL=[2,2,3]
;
My approach was to write a predicate that gets the first N elements of the list, write a second predicate that will act like a generator (calling the first predicate over and over until the list is shortened to size N), and then check for all of the generated sublists how many times there was match and get the maximum of that.
I got stuck on the generator predicate the rest I'm pretty sure I know how to write.
This is my code so far:
length([],0).
length([_|L],N) :- N is M+1, length(L,M).
// This will get the first N elements from the list.
// I tested it and it works.
sublist([H|_],1,[H]).
sublist([H|T],N,[H|LOP]) :- M is N-1, sublist(T,M,LOP).
// This is supposed to generate all the sublists,
// length is a predicate that returns the length of the list.
generator(L,N,L) :- length(L,M), N=:=M.
generator([H|T],N,[PN|LOP]) :- sublist([H|T],N,PN), generator(T,N,LOP).
This is the error I'm getting:
?- generator([1,2,3,4,5,6,7],2,X).
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [12] _6018 is _6024+1
ERROR: [11] length([1,2|...],_6052) at c:/users/ace_m/documents/prolog/bp.pl:44
ERROR: [10] generator([1,2|...],2,[1,2|...]) at c:/users/ace_m/documents/prolog/bp.pl:83
ERROR: [9] <user>
Exception: (10) generator([1, 2, 3, 4, 5, 6, 7], 2, _5204) ?
I understand that the error means that I'm not passing the correct values, but I don't understand where I'm going wrong. Any help
You can't use a variable in an arithmetic expression before it has been bound to a value.