以“原始" ./2格式显示列表 [英] Display a list in its 'raw' ./2 format
问题描述
是否可以以./2格式显示Prolog列表,例如
Is it possible to display a Prolog list in its ./2 format, e.g.
获取列表:
| ?- 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决定做不同的事情,所以这不是足够好:
Normally, write_canonical(List)
or ?- write_term(List, [quoted(true), ignore_ops(true)])
, as pointed out in the comments. Since SWI-Prolog decided to do things differently, this is not good enough:
?- write_canonical([a]).
[a]
true.
?- write_term([a], [quoted(true), ignore_ops(true)]).
[a]
true.
?- write_term([a], [dotlists(true)]).
.(a,[])
true.
请参见write_term/2
上的文档,请注意选项brace_terms(Bool)
和dotlists(Bool)
.但要注意:如果您正常启动SWI-Prolog 7,则./2
不再是列表函子!
See the documentation on write_term/2
, pay attention to the options brace_terms(Bool)
and dotlists(Bool)
. But beware: if you start SWI-Prolog 7 normally, the ./2
is not the list functor any more!
?- L = .(a, []).
ERROR: Type error: `dict' expected, found `a' (an atom) % WHAT?
?- L = '[|]'(a, []).
L = [a].
如果以swipl --traditional
开头,则一切恢复正常,
If you start it with swipl --traditional
, things are back to normal, sort of:
$ 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, ...]
在大多数常规情况下都足够好.
Read the linked section of the SWI-Prolog documentation for details and rationale. As an advice, if you decide to use SWI-Prolog stick to SWI-Prolog 7 with the defaults and only use write_term(List, [dotlists(true)])
if you need to communicate with another Prolog implementation. The usual list notation, [a, b, ...]
should be good enough in most conventional situations.
这篇关于以“原始" ./2格式显示列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!