序言中的非成员规则无法按预期工作 [英] not member rule in prolog doesn't work as expected

查看:29
本文介绍了序言中的非成员规则无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 prolog 中编写一个简单的迷宫搜索程序,在将房间添加到访问列表之前,我正在检查它是否已经是访问列表的成员.但是,即使我使用书中的代码,我也无法使其正常工作:

I'm trying to write a simple maze search program in prolog, before I add a room to visited list I'm checking whether it is already a member of the visited list. However, I can't get this to work, even if I use the code from the book:

d(a,b).
d(b,e).
d(b,c).
d(d,e).
d(c,d).
d(e,f).
d(g,e).


go(X, X, T).
go(X, Y, T) :-
    (d(X,Z) ; d(Z, X)),
    \+ member(Z,T),
    go(Z, Y, [Z|T]).

我做错了什么?

推荐答案

你的程序似乎没问题.我想问题是你在调用 go/3 时没有实例化第三个参数.在这种情况下, member(X, T) 将始终成功,从而使子句失败.

Your program seems to be ok. I guess the problem is that you are calling go/3 with the third argument uninstantiated. In that case it will member(X, T) will always succeed, thus failing the clause.

你可以用空列表作为第三个参数来调用你的谓词:例如

You might call your predicate with the empty list as the third parameter: e.g.

?- go(a, g, []).
true

如果要返回路径,请考虑添加另一个参数,如下所示:

If you want to return the path consider adding another parameter to go, like this:

go(From, To, Path):-
  go(From, To, [], Path).

go(X, X, T, T).
go(X, Y, T, NT) :-
    (d(X,Z) ; d(Z, X)),
    \+ member(Z,T),
    go(Z, Y, [Z|T], NT).

这篇关于序言中的非成员规则无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆