我有一个列表 L 创建为:
atomic_list_concat(L,' ', 'This is a string').
L = ['This',is,a,string]
现在我想使用成员函数在 L 中搜索一个原子。我试过了 :
?- member(' is',L).
L = [' is'|_G268] .
?- member( is,L).
L = [is|_G268] .
我在这里做错了什么?
我有一个列表 L 创建为:
atomic_list_concat(L,' ', 'This is a string').
L = ['This',is,a,string]
现在我想使用成员函数在 L 中搜索一个原子。我试过了 :
?- member(' is',L).
L = [' is'|_G268] .
?- member( is,L).
L = [is|_G268] .
我在这里做错了什么?
尽管dasblinkenlight发布的解决方案是正确的,但它在某种程度上打破了使用 Prolog 顶层的交互性质。通常,您希望下一个查询基于先前的解决方案。
出于这个原因,可以通过写where is a variable name 在先前查询中使用来重用顶级绑定。$Var
Var
在你的情况下:
?- atomic_list_concat(L, ' ', 'This is a string').
L = ['This', is, a, string].
?- member(' is', $L).
false.
?- member('is', $L).
true ;
false.
PS:请注意,搜索时您不会得到结果,' is'
因为分隔符被删除atomic_list_concat/3
。
您以交互方式运行的 Prolog 谓词不携带状态。当你跑
atomic_list_concat(L,' ', 'This is a string').
解释器向您展示了 的分配L
,然后忘记了它的值。当你member/2
在下一行运行时,L
又回到了它的自由变量状态。
如果您希望L
保留相同的内容,则需要保持在相同的请求中,如下所示:
:- atomic_list_concat(L,' ', 'This is a string'),
member(is, L),
writeln('membership is confirmed').
现在L
分配 fromatomic_list_concat
可用于member/2
,让它检查成员资格。