Prolog file manipulation problem

2019-09-01 05:00发布

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

标签: list file prolog
3条回答
狗以群分
2楼-- · 2019-09-01 05:10

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.

查看更多
爷的心禁止访问
3楼-- · 2019-09-01 05:14

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).
查看更多
forever°为你锁心
4楼-- · 2019-09-01 05:24

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.        

查看更多
登录 后发表回答