L]. I rewrote the body slightly to fix the problem and wound up with this code:

问题描述 投票:2回答:2
You need to have the parentheses around the whole condition or you'll get strange behavior. From there, I just removed the unnecessary

bindings (they can't work anyway because variables in Prolog are not assignables). It turns out once you fix these problems you'll find another problem, which is that you'll get this:TIf it's not obvious, this is because your base case is too vague and should instead look like this:FSo the final corrected version looks like this:truth_list(['abc','def'],['zui','def'],L).This is usually where @false shows up and points out that we have a problem using the predicate with different instantiations, so let's check that now and avoid some fury:L=['F','T']These all look alright, so it doesn't look like we're hallucinating lies when all the arguments are instantiated. That's good. Now let's check partial instantiations:

Cool, that worked.

truth_list([],[],_).
truth_list([H1|T1],[H2|T2],TL):-
    (H1==H2)->(H3='T');(H3='F'),
    Temp=TL,
    TL=[H3|Temp],
    truth_list(T1,T2,TL).

Eh. Well, it looks like Prolog doesn't know to hallucinate some other binding for that

value. Not sure if that's a problem or not but I don't see an obvious solution. That means the following probably won't work:
list prolog
2个回答
2
投票

: let's incorporate @false's improvement. Then we get the following:

L = ['F'|L].

Now we get the desired behavior:

Temp=TL,
TL=[H3|Temp],

So Prolog has inferred that Y is 'def', and concluded that X is at least not 'abc', so this is an improvement.

TL=[H3|TL]

truth_list([H1|T1],[H2|T2],[H3|TL]):-
    (H1=H2 -> H3='T' ; H3='F'),
    truth_list(T1,T2,TL).

TLmaplist

L = ['F', 'T'|_G297].

truth_list([], [], []).

我试图创建一个谓词,如果第三个列表由以下内容组成,则这个谓词为真。

truth_list([],[],[]).
truth_list([H1|T1],[H2|T2],[H3|TL]):-
    (H1=H2 -> H3='T' ; H3='F'),
    truth_list(T1,T2,TL).

?- truth_list(['abc','def'],['zui','def'],['T','F']).
false.
?- truth_list(['abc','def'],['zui','def'],['F','T']).
true.
?- truth_list(['abc','def'],['zui','def'],['F','T','T']).
false.
?- truth_list(['abc','def'],['zui','def','def'],['F','T','T']).
false.

值,取决于第一个和第二个列表中相同索引的两个元素是否相等。一个查询,如

?- truth_list([X, 'def'], ['abc', Y], ['T', 'T']).
X = abc,
Y = def.

应给

?- truth_list([X, 'def'], ['abc', Y], ['F', 'T']).
false.

.'F'这是我的尝试。

?- truth_list(X, Y, ['T', 'T']).
X = Y, Y = [_G296, _G302].

如果有人能提供一个解释,我将感激不尽, 为什么这不能像预期的那样工作。

truth_list([], [], []).
truth_list([H1|T1], [H2|T2], [H3|TL]) :-
  (H1 = H2, H3 = 'T' ; dif(H1,H2), H3 = 'F'),
  truth_list(T1, T2, TL).

我试图创建一个谓词,如果第三个列表由T和F值组成,那么这个谓词为真,这取决于第一个和第二个列表中的两个相同索引的元素是否相等。A ...

?- truth_list([X, 'def'], ['abc', Y], ['F', 'T']).
Y = def,
dif(X, abc) ;

所以我们都在同一页面上,当我运行查询时,我得到了这个结果。

所以我们首先想到的是你可能在重复使用一个变量 事实上,下面的句子看起来很可疑。

2
投票

这就是为什么我们的结果看起来是这样的: L=['F'4可以简化你的代码。

truth_list(A, B, C) :-
  maplist(truth_, A, B, C).
truth_(A, B, C) :-
  A == B -> C = 'T' ; C = 'F'.

你也可以考虑增强你的代码,以便(最终)重用。

你可以用以下的方法来代替任意的常量'T','F'。可调用 谓词,如 truefalse1, 0 以获得可直接用于CLP(FD)的表达式。

© www.soinside.com 2019 - 2024. All rights reserved.