Prolog代码给出了两个不同的结果

时间:2015-05-15 01:10:14

标签: list prolog

所以我正在制作这个代码,其中有一个接收2个参数的函数,并告诉其中一个参数不是列表。

代码如下:

/*** List Check ***/
islist(L) :- L == [], !.
islist(L) :- nonvar(L), aux_list(L).
aux_list([_|_]).

/*** Double List Check ***/
double_check(L, L1) :- \+islist(L) -> write("List 1 invalid"); 
    \+islist(L1)-> write("List 2`invalid"); write("Success").

它很有效。 Online代码正是我想要的。但是在我的计算机的Prolog控制台上,它提供了一个完全不同的答案:

?- double_check(a, [a]).
[76,105,115,116,97,32,49,32,105,110,118,97,108,105,100,97] 
true.

例子。我没有IDEA来自哪个列表。有人能告诉我我的错误并帮我解决吗?谢谢大家!

1 个答案:

答案 0 :(得分:2)

快速修复:使用format/2代替write/1! 有关内置谓词format/2click here

的详细信息
$ swipl --traditional
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.37) [...]

?- write("abc").
[97,98,99]                                % output by write/1 via side-effect
true.                                     % truth value of query (success)

?- format('~s',["abc"]).
abc                                       % output by format/2 via side-effect
true.                                     % truth value (success)

但是使用不同的命令行参数:

$ swipl 
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.37) [...]

?- write("abc").
abc
true.

?- format('~s',["abc"]).
abc
true.

尽管看起来有点令人讨厌,但我建议将SWI-Prolog的命令行选项--traditionalformat/2结合使用,而不是write/1保留

相关问题