开发者

Set parameters dynamically in Prolog

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),
    fin开发者_如何学Cdall(Second, data(_, Second, _), L2),
    findall(Third, data(_, _, Third), L3).


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

similar question here btw



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).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜