序言和清单统一 [英] Prolog and List Unification
问题描述
我正在努力加深我对Prolog的理解,以及它如何处理统一性.在这种情况下,它如何处理与列表的统一.
I'm trying to further my understanding of Prolog, and how it handles unification. In this case, how it handles unification with lists.
这是我的知识库;
member(X, [X|_]).
member(X, [_|T]):- member(X, T).
如果我正确地理解了该过程.如果member(X, [X|_])
不是true,则进入递归规则,如果X
在列表T
中,则[_|T]
与T
统一.
If I'm understanding the process correctly. If member(X, [X|_])
is not true, then it moves into the recursive rule, and if X
is in list T
, then [_|T]
is unified with T
.
那么我的递归谓词中的匿名变量会怎样?它会被丢弃吗?我很难理解列表的确切统一过程,因为[_|T]
是两个变量,而不是一个.我只是想弄清楚统一过程如何与列表一起精确地工作.
So what happens to the anonymous variable in my recursive predicate? Does it get discarded? I'm having difficulty understanding the exact unification process with lists, as [_|T]
is two variables, rather than one. I'm just trying to figure out how the unification process works precisely with lists.
推荐答案
假定_
是Y
member(X, [Y|T]):- member(X, T).
这就是True
,而不管Y
.现在,您正在返回" member(X, T)
.换句话说,您将丢弃Y
并返回" member(X, T).
Then this is True
regardless Y
. Now you are "returning" member(X, T)
. In other words, you are discarding Y
and "returning" member(X, T).
_
意味着,无论它是什么,都将忽略该变量.
_
means, whatever it is, ignore that variable.
_就像其他任何变量一样,除了您看到的每个变量都是 视为不同的变量,Prolog不会向您显示它的含义 与统一.那里没有特殊的行为.如果让您感到困惑 关于行为,只需发明一个全新的变量并将其放入 在那里看看它做什么.
The _ is just like any other variable, except that each one you see is treated as a different variable and Prolog won't show you what it unifies with. There's no special behavior there; if it confuses you about the behavior, just invent a completely new variable and put it in there to see what it does.
在这种情况下,您的函数将检查列表中是否存在给定元素,因此,获取列表中的第一个元素,检查是否相等,如果不相等,则丢弃该元素并继续前进.
In your case, your function check if a given element exists on a list, so, you take first element of the list, check if is equal, if not, you discard that element and moves on.
这篇关于序言和清单统一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!