0

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).
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
same
  • 51
  • 8
  • possible duplicate of [How to generalize program according to arity in prolog?](http://stackoverflow.com/questions/5949012/how-to-generalize-program-according-to-arity-in-prolog) – Fred Foo May 11 '11 at 06:31

3 Answers3

1

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.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
0

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).
尾崎隆大
  • 148
  • 1
  • 1
0

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.        

尾崎隆大
  • 148
  • 1
  • 1