如果逻辑编程中的语句,如何链多个链接?
我想在收到新状态后更新我的知识库,
not((hasBeenVisited(X-1, Y)); not(wall(X-1, Y)) -> asserta(isDangerous(X-1, Y));
not(((hasBeenVisited(X+1, Y)); not(wall(X+1, Y)) -> asserta(isDangerous(X+1, Y));
not((hasBeenVisited(X, Y-1)); not(wall(X, Y-1)) -> asserta(isDangerous(X, Y-1));
not((hasBeenVisited(X, Y+1)); not(wall(X, Y+1)) -> asserta(isDangerous(X, Y+1));
我的代码问题是,如果第一行评估为true,那么由于逻辑或“”;
如果我要改变“”;逻辑和“”,然后,如果其中一个条件失败,则整个谓词返回false而不是true。
我如何链多个IFS语句?在程序编程中,我们可以做这样的事情:
if condition1 then statements1;
if condition2 then statements2;
if condition3 then statement3;
...
我什至应该用prolog做到这一点,因为我仍在考虑程序编程方面...
I want to update my knowledge base after receiving a new state,
not((hasBeenVisited(X-1, Y)); not(wall(X-1, Y)) -> asserta(isDangerous(X-1, Y));
not(((hasBeenVisited(X+1, Y)); not(wall(X+1, Y)) -> asserta(isDangerous(X+1, Y));
not((hasBeenVisited(X, Y-1)); not(wall(X, Y-1)) -> asserta(isDangerous(X, Y-1));
not((hasBeenVisited(X, Y+1)); not(wall(X, Y+1)) -> asserta(isDangerous(X, Y+1));
problem with my code is that if the first line evaluates to true, then, the next lines are not evaluated, because of the logical OR ";".
If I were to change the ";" to logical AND ",", then if one of the conditions fail, the entire predicate returns false instead of true.
How do I chain multiple IFs statements? In procedural programming, we can do something like this:
if condition1 then statements1;
if condition2 then statements2;
if condition3 then statement3;
...
Should I even do that with prolog because I am still thinking in terms of procedural programming...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的命令式示例:
变为此形状:
将“代码块”包裹在()parens中,并确保条件是否持续不存在,因此块是正确的,因此块是正确的,并且下一个块可以运行。
您是说对的,以这种方式进行编码和状态更新是在与序言的设计作斗争,而不是依靠其优势。另外
x+1
无法按照您使用的方式工作,我想知道您是否认为isdangeric(x+1,y)
会像这是一个函数调用,该返回将进入asserta(< tere>)
?如果是这样,那也不会发生。您可能被推向迷宫的动作列表,应该依靠Prolog的回溯来寻找墙壁和危险的地方,然后向后退后一步,然后再走上一路。
Your imperative example:
becomes this shape:
with "code blocks" wrapped in () parens and making sure there is always a true whether the condition holds or not, so the block is true AND the next block can run.
You're right that coding in this way with imperative rules and state updates is fighting against the design of Prolog, and not leaning on its strengths. Also
X+1
doesn't work the way you are using it, and I wonder if you are thinking thatisDangerous(X+1, Y)
will return a value like it was a function call and that return goes intoasserta(<here>)
? If so, that won't happen either.Probably what you are being pushed towards is building a list of movements to get through a maze, and should be leaning on Prolog's backtracking to find the walls and dangerous places, step back from them and go another way.