简体   繁体   English

Prolog 简单谓词,如何?

[英]Prolog simple predicate , How?

I have a predicate:我有一个谓词:

neig(I, J, I1, J1):-
    I1 is I - 1,
    I1 >= 0,
    J1 is J.
neig(I, J, I1, J1):-
    I1 is I + 1,
    not(I1 > 8),
    J1 is J.
neig(I, J, I1, J1):-
    J1 is J - 1,
    J1 >= 0,
    I1 is I.
neig(I, J, I1, J1):-
    J1 is J + 1,
    not(J1 > 8),
    I1 is I.

neig(I, J, I1, J1):-
    I1 is I - 1,
    J1 is J - 1,
    I1 >= 0,
    J1 >= 0.
neig(I, J, I1, J1):-
    I1 is I + 1,
    J1 is J + 1,
    not(I1 > 8),
    not(J1 > 8).
neig(I, J, I1, J1):-
    I1 is I + 1,
    J1 is J - 1,
    J1 >= 0,
    not(I1 > 8).
neig(I, J, I1, J1):-
    I1 is I - 1,
    J1 is J + 1,
    I1 >= 0,
    not(J1 > 8).

How to write predicate all_neighs(I, J, L) where L is as list and it contains all different elements [I1, J1] , such that neigh(I, J, I1, J1) ?如何编写谓词all_neighs(I, J, L)其中L是列表,它包含所有不同的元素[I1, J1] ,例如neigh(I, J, I1, J1)

I think that you need this build-in predicate.我认为你需要这个内置谓词。

findall(Things,GoalCondition, Bag)

Which would then look something like this:然后看起来像这样:

all_neighs(I,J,L) :- findall([H|T],neig(I,J,H,T), L).

you may have to check if T is an atom if that's what you want.如果这是你想要的,你可能必须检查 T 是否是一个原子。 But with this my result with some examples.但是有了这个我的结果和一些例子。

1 ?- all_neighs(0,0,X).
X = [[1|0], [0|1], [1|1]].

2 ?- all_neighs(1,1,X).
X = [[0|1], [2|1], [1|0], [1|2], [0|0], [2|2], [2|0], [0|...]].

You should also take a look at this: [1] it explains on how you can easily implement the findall(...) predicate yourself.您还应该看看这个:[1] 它解释了如何轻松地自己实现 findall(...) 谓词。

[1] http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_12.html [1] http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_12.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM