Prolog 代码给出了两个不同的结果
Prolog code gives two different results
所以我正在编写这段代码,其中有一个函数接收 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.
例子。我不知道那个名单是从哪里来的。有人可以告诉我我的错误并帮我解决吗?谢谢大家!
快速修复:使用 format/2
而不是 write/1
!
有关内置谓词 format/2
、click 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 使用命令行选项 --traditional
,并结合 format/2
而不是 write/1
。 保留 portability!
所以我正在编写这段代码,其中有一个函数接收 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.
例子。我不知道那个名单是从哪里来的。有人可以告诉我我的错误并帮我解决吗?谢谢大家!
快速修复:使用 format/2
而不是 write/1
!
有关内置谓词 format/2
、click 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 使用命令行选项 --traditional
,并结合 format/2
而不是 write/1
。 保留 portability!