gpt4 book ai didi

list - 以 'raw' ./2 格式显示列表

转载 作者:行者123 更新时间:2023-12-04 19:56:28 25 4
gpt4 key购买 nike

是否可以以 ./2 格式显示 Prolog 列表,例如

对于列表:

| ?- L=[a,b,c].
L = [a,b,c] ?
yes

有没有办法显示:
L = .(a, .(b, .(c, []))).

最佳答案

通常,write_canonical(List)?- write_term(List, [quoted(true), ignore_ops(true)]) ,正如评论中指出的那样。由于 SWI-Prolog 决定 do things differently ,这还不够好:

?- write_canonical([a]).
[a]
true.

?- write_term([a], [quoted(true), ignore_ops(true)]).
[a]
true.

?- write_term([a], [dotlists(true)]).
.(a,[])
true.

documentation on write_term/2 ,注意选项 brace_terms(Bool)dotlists(Bool) .但要注意:如果您正常启动 SWI-Prolog 7, ./2不再是列表仿函数了!
?- L = .(a, []).
ERROR: Type error: `dict' expected, found `a' (an atom) % WHAT?

?- L = '[|]'(a, []).
L = [a].

如果您以 swipl --traditional 开头,事情恢复正常,有点:
$ swipl --traditional
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.4-32-g9311e51)
Copyright (c) 1990-2015 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- L = .(a, []).
L = [a].

您仍然无法使用 write_canonical(List)write_term(List, [quoted(true), ignore_ops(true)]) .

阅读 SWI-Prolog 文档的链接部分以了解详细信息和基本原理。作为建议,如果您决定使用 SWI-Prolog,请坚持使用默认设置的 SWI-Prolog 7,并且只使用 write_term(List, [dotlists(true)])如果您需要与另一个 Prolog 实现进行通信。通常的列表符号, [a, b, ...]在大多数常规情况下应该足够好。

关于list - 以 'raw' ./2 格式显示列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31939658/

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