I use swi prolog and my code like this.
I read data predicate from file and its arity count can change. How can I generalize it. For example, If data(a1,b1,c1) writes in the file, how can I find solution? Do you have any idea?
> basla:-consult('test.pl'),
> answer(L1,L2,L3,L4,L5),
> list_to_set(L1, X),
>
> write(X).
> answer(L1,L2,L3,L4,L5):-
> findall(First, data(First,_,_,_,_),L1),
> findall(Second, data(_,Second,_,_,_),L2),
> findall(Third, data(_,_,Third,_,_),L3).
If the arity of a predicate seems to change, it's almost always better to make it arity one and give it a list argument. Your findall
queries can then be extended with a call to nth1
or nth0
.
basla(Predicate/Arity) :-
consult('test.pl'),
length(L,Arity),
for(1,Nth,Arity),
findall(A,(
nth1(Nth,L,A),
P =.. [Predicate|L],
call(P)),
LX),
list_to_set(LX,U),
writef('%t\n',[U]),
Nth = Arity.
for(B,C,A) :-
A >= B,
for_2(B,C,A).
for(B,C,A) :-
A B,!,
fail.
for_2(A,A,_).
for_2(A,C,D) :-
B is A + 1,
for_2(B,C,D).
basla(Predicate/Arity) :-
consult('test.pl'),
length(L,Arity),
for(1,Nth,Arity),
findall(A,(
nth1(Nth,L,A),
P =.. [Predicate|L],
call(P)),
LX),
list_to_set(LX,U),
writef('%t\n',[LX]),
Nth = Arity.