gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:37:38 25 4
gpt4 key购买 nike

所以我正在编写这段代码,其中有一个函数接收 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 而不是 写/1保留 !

关于list - Prolog 代码给出了两个不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30249969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com