我正在尝试创建以下Prolog代码的可执行文件:
item(one, 50, 40).
item(two, 80, 70).
item(three, 100, 55).
item(four, 50, 45).
maxMeanStudy:-
findall(M,item(_,M,_),L),
max_member(Max,L),
write("Maximum mean value:"), % this line is not printed properly.
writeln(Max),!.
main:-
maxMeanStudy.
我正在使用以下命令来创建此页面上提到的可执行文件:http : //www.swi-prolog.org/pldoc/man?section=cmdlinecomp
$ swipl --goal=main --stand_alone=true -o myprog -c myques.pl
创建的可执行文件不会以字母形式写入字符串“ Maximum mean value:”,而是以代码形式写入:
$ ./myprog
[77,97,120,105,109,117,109,32,109,101,97,110,32,118,97,108,117,101,58]100
我正在Linux(32位)上工作。我怎么解决这个问题。谢谢你的帮助。
在几乎所有此类情况下,事实证明:
您不应该首先使用副作用。而是定义您可以实际推理的关系。在您的情况下,您正在描述平均值与最大值之间的关系。因此,这个名字maximum_mean_value(Ms, M)
表明了自己。而is_that_not_more_readable
比mixingLowerAndUpperCaseLetters
?
让顶层通过纯查询(例如)为您完成打印:
?- maximum_mean_value(Ms, M).
M = ... . % result is automatically shown by the toplevel!
如果您确实需要在终端上编写某些内容,请在单独的谓词中进行操作。避免混合纯净和不纯净的代码。
使用format/2
格式化输出。例如:
maximum_mean_value(Ms, M),
format("Maximum mean value: ~d\n", [M])
请注意如何format/2
轻松输出涉及其他术语的文本。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句