以“原始”./2格式显示列表

时间:2015-08-11 10:45:41

标签: list prolog

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

列表:

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

有没有办法展示:

L = .(a, .(b, .(c, []))).

1 个答案:

答案 0 :(得分:4)

通常,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,并且如果您需要与另一个Prolog实现进行通信,则仅使用write_term(List, [dotlists(true)])。在大多数传统情况下,通常的列表符号[a, b, ...]应该足够好。