Prolog 可执行文件在代码中写入字符串

Prolog executable writing String in codes

我正在尝试创建以下 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 位)。我怎么解决这个问题。感谢您的帮助。

在几乎所有此类情况下,事实证明:

  1. 首先你不应该使用副作用。相反,定义您可以实际推理的 关系 。在您的情况下,您描述的是平均值与其最大值之间的关系。因此,名称 maximum_mean_value(Ms, M) 本身就是一个意思。而且 is_that_not_more_readablemixingLowerAndUpperCaseLetters?

  2. 让顶层为您打印,通过像这样的纯查询:

    ?- maximum_mean_value(Ms, M).
    M = ... . % result is automatically shown by the toplevel!
    
  3. 如果您真的需要在终端上写东西,请在单独的谓词中进行。 避免混合纯代码和不纯代码。

  4. 使用format/2格式化输出。例如:

    maximum_mean_value(Ms, M),
    format("Maximum mean value: ~d\n", [M]) 
    

    请注意 format/2 如何轻松输出涉及其他术语的文本。