一些隐式约束 [英] Some implicit constraint

查看:59
本文介绍了一些隐式约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行这段代码(如下所示)时,它总是设置隐式约束.

When i execute this code(shown below) , it always sets implicit kind of constraint.

正如你在下面看到的,它总是说 D1 = D2 但没有这种明确的约束,也没有任何模式匹配来强制这样做.

As you can see below , it always says that D1 = D2 but there is no such explicit constraints nor any pattern matching which forces this.

或者换句话说,D1D2 之间有一些引用,这样每当 D1 被初始化时,D2被自动初始化.我看不出这是怎么回事.有人能解释一下吗,我试着用调试器弄明白了,但没有帮助.

Or in otherwords there is some reference between D1 and D2 such that whenever D1 gets initialized, D2 gets automatically initialized. I can't see how this is happening. Can someone explain me, i tried ot figure it out with debugger but it did not help.

这是一个谜题GERALD + DONALD = ROBERT",最初三个列表包含这些变量.

It's a puzzle "GERALD + DONALD = ROBERT", initially three lists contains these variables.

如果有人想测试它,我会在下面添加代码:

I add code below if anyone wants to test it:

sum(N1,N2,N):-
    sum1(N1,N2,N,0,0,[0,1,2,3,4,5,6,7,8,9],_).

sum1([],[],[],0,0,Digits,Digits).    
sum1([D1|N1],[D2|N2],[D|N],C1,C,Digs1,Digs2):-
    sum1(N1,N2,N,C1,C2,Digs1,Digs2),
    digitSum(D1,D2,C2,D,C,Digs2,Digs).

digitSum(D1,D2,C1,D,C,Digs1,Digs):-
    del(D1,Digs1,Digs2),
    del(D2,Digs2,Digs3),
    del(D,Digs3,Digs),
    S is D1 + D2 + C1,
    D is S mod 10,
    C is D div 10.

del(A,L,L):-
    nonvar(A),!.
del(A,[A|L],L).
del(A,[B|L],[B|L1]):-
    del(A,L,L1).

查询:

?- sum( [D,O,N,A,L,D], [G,E,R,A,L,D], [R,O,B,E,R,T] ).

推荐答案

当它说 D1 = D2 那么你应该看到 D1D2 列表中有相同的变量,List 是一个函子,一个变量在整个函子中可见.

When it says D1 = D2 then you should see that both D1 and D2 have same variable from the list and List is a functor and one variable is visible in whole functor.

你应该看到,当递归结束时,你有来自 GERALDDONALDD's(最后一个元素),因为这个 D在整个函子中可见,D1D2 都指向同一个变量.

You should see that when the recursion ends, then you have have D's(last element) from GERALD and DONALD , since this D is visible in whole functor, D1 and D2 both refer to same variable.

这篇关于一些隐式约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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