-1

I'm trying to make my code more dynamic. I have a file with the following contents:

a(b1, c1, d1).
a(b2, c2, d2).
a(b3, c3, d3).

And as I find all b1, I make a list like this:

[b1, b2, b3].

When the arity changes in the file, for example, when a(b1,c1,d1) becomes a(b1,c1,d1,e1), my code does not work. Is there a way to solve this problem? I'm using SWI-Prolog.

start :-
    consult('file.pl'),
    solve(L1, L2, L3),
    list_to_set(L1, X),
    write(X).

solve(L1, L2, L3):-
    findall(First, data(First, _, _), L1),
    findall(Second, data(_, Second, _), L2),
    findall(Third, data(_, _, Third), L3).
Rubens
  • 14,478
  • 11
  • 63
  • 92
Tolga
  • 9
  • 2
  • 1
    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 13 '11 at 08:33

2 Answers2

0

if the arity changes you should put the arguments in a list and use nth1/3

similar question here btw

Community
  • 1
  • 1
Thanos Tintinidis
  • 5,828
  • 1
  • 20
  • 31
0

start:-
        consult('file.pl'),
        sampling(Arity),
        solve(Arity,LL),
        list_to_set(LL, X),
        write(X).

solve(Arity,LL):-
        length(L,Arity),
        P =.. [data|L],
        findall(L1,(
                   for(1,N,Arity),
                   nth1(N,L,A),
                   findall(A,P,L1)),
                LL).

sampling(Arity) :-
        see('file.pl'),
        read(P),
        sampling(P,Arity),
        seen.

sampling(end_of_file,_) :- !,seen,fail.
sampling(P,Arity) :-
        functor(P,data,Arity),!.
sampling(_,Arity) :-
        read(P),
        sampling(P,Arity).
尾崎隆大
  • 148
  • 1
  • 1