以“原始"./2 格式显示列表 [英] Display a list in its 'raw' ./2 format

查看:10
本文介绍了以“原始"./2 格式显示列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以以 ./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)]).

You still cannot use write_canonical(List) or 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆