检查变量是否为空或已填充 [英] Check if variable is empty or filled
问题描述
我有以下问题:
prolog prog:
man(thomas, 2010).
man(leon, 2011).
man(thomas, 2012).
man(Man) :- once(man(Man, _).
问题:
?- man(thomas).
true ; %i want only on true even if there are more "thomas" *working because of once()*
?- man(X).
X = thomas ; %i want all man to be listed *isn't working*
目标:
?- man(thomas).
true ;
?- man(X).
X = thomas ;
X = leon ;
X = thomas ;
我不明白为什么会发生这种情况,但仍然想得到所有人的名字.所以我的解决方案是查看Man"是否已初始化,如果是,则不是once..",否则......类似这样的:
I do unterstand why this happens, but still want to get the names of all man. So my solution woud be to look if "Man" is initialized, if yes than "once.." else then... something like that:
man(Man) :- (->check<-,once(man(Man, _)); man(Man, _).
在check"上应该是检查变量Man"是否被填充的代码.
On "check" shoud be the code sniped that checks if the variable "Man" is filled.
这可能吗?
推荐答案
一种实现方式如下:
man(X) :-
(nonvar(X), man(X, _)), !
;
man(X, _).
或者,更优选的是:
man(X) :-
( var(X)
-> man(X, _)
; once(man(X, _))
).
切割将确保对实例化的 X
只有一个解决方案(至多),而非实例化的情况将顺其自然.请注意,通过剪切,您不需要 once/1
.once/1
在没有剪切的情况下无法按预期工作的原因是回溯仍然会返回并采用或"条件并在那里成功.
The cut will ensure only one solution (at most) to an instantiated X
, whereas the non-instantiated case will run its course. Note that, with the cut, you don't need once/1
. The reason once/1
doesn't work as expected without the cut is that backtracking will still come back and take the "or" condition and succeed there as well.
这篇关于检查变量是否为空或已填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!