本文介绍了Prolog“单例变量"警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Prolog 的新手并且非常困惑!我不断收到[WMAPDY] 的单例变量"警告.我在某处读到有时警告是无用的.我还读到程序不会因为警告而编译所有子句?

I'm new to Prolog and royally confused! I keep getting a "singleton variable for [WMAPDY]" warning. I read somewhere that sometimes that warning is useless. I also read that the program will not compile all the clauses because of the warning?

我正在尝试做的程序是一个密码算术难题,应该解决"AM+PM=DAY.

The program I'm trying to do is a crypt-arithmetic puzzle that is supposed to "solve" AM+PM=DAY.

如果有人能帮助解决这个错误,并且单例变量警告总是很重要,我将不胜感激!

If anyone could help with this error and also wether the singleton variable warning is always important I'd greatly appreciate it!!

斯科特

solve([A,M,P,D,Y]):- 
select(A,[0,1,2,3,4,5,6,7,8,9],WA), % W means Without
not(A=0),
select(M,WA,WMA),
select(P,WMA,WMAP),
not(P=0),
select(D,WMAP,WMAPD),
not(D=0),
select(Y,WMAPD,WMAPDY),
DAY is 100*D+10*A+Y,
AM  is 10*A+M,
PM  is 10*P+M,
DAY is AM+PM.

推荐答案

由于这一行而产生警告:

The warning is generated because of this line:

select(Y,WMAPD,WMAPDY),

程序不会在其他任何地方使用变量 WMAPDY,因此它没有用,Prolog 会警告您,因为它可能是一个错字(在这种情况下不是).要摆脱警告,您有一些可能性:

The program doesn't use the variable WMAPDY anywhere else, thus it is useless, and Prolog warns you about it, because it is likely a typo (it isn't in this case). To get rid of the warning you have some possibilities:

  1. 使用 member/2 而不是 select/3,因为您对结果列表不感兴趣:member(Y,WMAPD).

  1. Use member/2 instead of select/3, since you aren't interested in the resulting list: member(Y,WMAPD).

将变量标记为单例.如果您以 _ 开始变量,它们将不会被检查,因为它们是单例:select(Y, WMAPD,_WMAPDY).或者,您可以使用特殊的单例变量 _:select(Y,WMAPD,_).(此描述至少对于 SWI Prolog 是正确的,下划线变量 _WMAPDY 可能适用于更多方言.

Mark the variable as a singleton. If you start variables with a _ they wont be checked it they are singletons: select(Y, WMAPD,_WMAPDY). Alternatively, you could use the special singleton variable _: select(Y,WMAPD,_). (This description is at least true for SWI Prolog, the underscored variable _WMAPDY might work with more dialects).

在您的文件中使用 :- style_check(-singleton).这将关闭文件的所有单例变量警告,我宁愿不使用它,因为此警告有助于查找拼写错误.(此说明也适用于 SWI Prolog,SICStus Prolog 可能会使用选项 single_var_warnings,对于其他系统,请查看您的手册).

Use :- style_check(-singleton) in your file. This turns off all singleton variable warnings for the file, I'd rather not use that, because this warning is good for finding typos. (this desciption also is for SWI Prolog, SICStus Prolog may use the option single_var_warnings, for other systems, check your manual).

这是 SWI-Prolog 手册中的相关部分

这篇关于Prolog“单例变量"警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:18