SWI Prolog陷入了奇怪的行为
事情是这样的,我想用prolog编写一个“完美匹配”的程序。 你给它一对夫妇,每个人都有一些“特征”。这个程序应该是 根据这些特征返回未来婚姻中可能出现的问题。但当我尝试这样做,事情变得复杂时,我遇到了这个问题:SWI Prolog陷入了奇怪的行为,prolog,Prolog,事情是这样的,我想用prolog编写一个“完美匹配”的程序。 你给它一对夫妇,每个人都有一些“特征”。这个程序应该是 根据这些特征返回未来婚姻中可能出现的问题。但当我尝试这样做,事情变得复杂时,我遇到了这个问题: 1 ?- marriage_problems(Couple). Wifename(sharon,marsh) Husbandname(randy,marsh) Possible problem:Wife is lazy while husband is demanding.Not ma
1 ?- marriage_problems(Couple).
Wifename(sharon,marsh)
Husbandname(randy,marsh)
Possible problem:Wife is lazy while husband is demanding.Not matching.
true ;
Possible problem: Wife is agressive while husband is sensitive.Not matching.
true ;
Possible problem: Wife is sensitive while husband is agressive.Not matching.
true ;
Possible problem: Wife is sensitive while husband is agressive.Not matching.
true ;
Possible problem: Wife is sensitive while husband is agressive.Not matching.
true ;
Possible problem: Wife is sensitive while husband is agressive.Not matching.
true
这永远不会停止,丈夫不匹配时妻子很敏感。这也是错误的。下面是代码:
couple(wife(name(sharon,marsh),char(economical([stingy,lazy]),sexual([agressive]))),
husband(name(randy,marsh),char(economical([demanding,wasteful]),sexual([sensitive])))).
marriage_problems(Couple):- couple(wife(Wname,char(economical(W_eco),sexual(W_sex))),
husband(Hname,char(economical(H_eco),sexual(H_sex)))),
write('Wife'),write(Wname),nl,
write('Husband'),write(Hname),nl,
marriage_economical_problems(W_eco,H_eco);
marriage_sexual_problems(W_sex,H_sex).
marriage_economical_problems(W_eco,H_eco):- couple(wife(_,char(economical(W_eco),_)),husband(_,char(economical(H_eco),_))),
member(demanding,W_eco),
member(lazy,H_eco),
W_pr=demanding,
H_pr=lazy,
write('Possible problem:Wife is '),write(W_pr),write(' while husband is '),
write(H_pr),write('.Not matching.'),nl;
member(lazy,W_eco),
member(demanding,H_eco),
W_pr=lazy,
H_pr=demanding,
write('Possible problem:Wife is '),write(W_pr),write(' while husband is '),
write(H_pr),write('.Not matching.'),nl.
marriage_sexual_problems(W_sex,H_sex):- couple(wife(_,char(_,sexual(W_sex))),husband(_,char(_,sexual(H_sex)))),
member(agressive,W_sex),
member(sensitive,H_sex),
W_pr=agressive,
H_pr=sensitive,
write('Possible problem: Wife is '),write(W_pr),write(' while husband is '),
write(H_pr),write('.Not matching.'),nl;
member(sensitive,W_sex),
member(agressive,H_sex),
W_pr=sensitive,
H_pr=agressive,
write('Possible problem: Wife is '),write(W_pr),write(' while husband is '),
write(H_pr),write('.Not matching.'),nl.
这一点很奇怪:
1 ?- marriage_problems(Couple).
Wifename(sharon,marsh)
Husbandname(randy,marsh)
Possible problem:Wife is lazy while husband is demanding.Not matching.
true ;
Possible problem: Wife is agressive while husband is sensitive.Not matching.
true.
代码:
因此,当它变得更复杂时,由于某种原因它就不起作用了。。
我真的不明白发生了什么事。我只熟悉几个月
序言。我自己已经处理这个问题好几天了,运气不好。但是现在
我的时间不多了,我需要把事情做完。任何帮助都将不胜感激。
提前谢谢。您可能误用了OR运算符(;)/2,您的意思是
marriage_problems(Couple):- couple(wife(Wname,char(economical(W_eco),sexual(W_sex))),
husband(Hname,char(economical(H_eco),sexual(H_sex)))),
write('Wife'),write(Wname),nl,
write('Husband'),write(Hname),nl,
( marriage_economical_problems(W_eco,H_eco);
marriage_sexual_problems(W_sex,H_sex) ).
如果没有父母,当称婚姻为“性问题”时,“W_sex”和“H_sex”是不受约束的。
SWI Prolog能够发出症状信号:
Warning: /home/carlo/prolog/so.pl:81:
Singleton variable in branch: W_sex
Singleton variable in branch: H_sex
Warning: /home/carlo/prolog/so.pl:81:
Singleton variables: [Couple]
还要注意的是,夫妻是无用的……我很想说,这是因为连计算机都无法理解婚姻和人类求爱的复杂程度。谢谢你的回答。它解决了我的问题。正如我所说,我对prolog非常陌生,我还不太明白它的逻辑。我真的不明白为什么现在还需要Paren。。但它确实有效,谢谢。此外,我还想使用Couple,因为稍后我将通过prolog的命令提示符输入具有不同“字符”的不同Couple,但现在这似乎也不可能了,作为优先级的旁白,有一点我有点困惑,
*
的优先级高于+
,因此X是2+3*4
产生X=14
(由于*
具有更高的优先级,因此优先考虑*
)。然而,
的优先级高于,
,但为false,为true;真
和真;true、false
每个结果都是true
,这意味着Prolog在每种情况下都将,
放在第一位。我希望这意味着,
的优先级更高。我的思维错误在哪里?@潜伏者:这给了(+)/2 500优先权,而(*)/2有400优先权。。。看起来不错。我认为“优先”一词可能会产生误导,当我最初发布答案时,我告诉(Wrongy)这一点;它的优先级低于,你现在让我很困惑,哈哈。那么prolog是否优先于and(,)或(;)?@dieciminal,,
(and)确实优先于代码>(或),因此需要括号,如图所示。我们的讨论超越了“优先”这个词。这意味着,数字越高,优先级越高,这就是我开始困惑的地方。我认为更高的优先级意味着它优先,这意味着他们的陈述是不正确的。
Warning: /home/carlo/prolog/so.pl:81:
Singleton variable in branch: W_sex
Singleton variable in branch: H_sex
Warning: /home/carlo/prolog/so.pl:81:
Singleton variables: [Couple]