如何使用 SWI-Prolog ./2 功能? [英] How to use SWI-Prolog ./2 function?
问题描述
需要使用 SWI-Prolog 的示例 ./2.
Need example using SWI-Prolog ./2.
签名是
.(+Int,[])
此外,如果有此运算符的名称,我会很高兴知道.搜索."毫无意义.
Also if there is a name for this operator it would be nice to know. Searching for '.' is senseless.
最接近名称的是 SWI 文档第 F.3 节 算术函数
The closest to a name is from SWI documentation section F.3 Arithmetic Functions
List of one character: character code
我尝试了什么
?- X is .(97,[]).
结果
ERROR: Type error: `dict' expected, found `97' (an integer)
ERROR: In:
ERROR: [11] throw(error(type_error(dict,97),_5812))
ERROR: [8] '<meta-call>'(user:(...,...)) <foreign>
ERROR: [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
也试过了
?- X is .(97,['a']).
结果相同.
推荐答案
在标准 Prolog 中,'.'
函子是实际的列表项函子.具体来说,[H|T]
等价于 '.'(H, T)
.例如,如果你运行 GNU Prolog,你会得到:
In standard Prolog, the '.'
functor is the actual list term functor. Specifically, [H|T]
is equivalent to '.'(H, T)
. If you run GNU Prolog, for example, you will get this:
| ?- write_canonical([H|T]).
'.'(_23,_24)
yes
| ?- X = .(H,T).
X = [H|T]
yes
| ?-
is/2
运算符将允许您直接从由单个字符代码组成的列表中读取字符代码.所以以下在 Prolog 中有效:
The is/2
operator will allow you to read the character code directly from a list consisting of a single character code. So the following works in Prolog:
| ?- X is [97].
X = 97
yes
| ?- X is .(97,[]).
X = 97
yes
开始变得有趣的是,在 SWI Prolog 中,他们引入了 dictionaries 依赖于 '.'
作为指定字典键的方法.这会与用作列表仿函数的 '.'
仿函数产生冲突.这在字典的链接中进行了解释.因此,当您尝试将 '.'
函子用于列表时,您会得到如下结果:
Where it starts to get interesting is that, in SWI Prolog, they've introduced dictionaries that rely on the '.'
as a means of designating dictionary keys. This creates a conflict with the '.'
functor being used as a list functor. This is explained in the link on dictionaries. Thus, when you attempt to use the '.'
functor for a list, you get something like this:
1 ?- X is [97].
X = 97.
2 ?- X is .(97, []).
ERROR: Type error: `dict' expected, found `97' (an integer)
ERROR: In:
ERROR: [11] throw(error(type_error(dict,97),_5184))
ERROR: [9] '$dicts':'.'(97,[],_5224) at /usr/local/lib/swipl-7.4.2/boot/dicts.pl:46
ERROR: [8] '<meta-call>'(user:(...,...)) <foreign>
ERROR: [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
3 ?- X = .(H,T).
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [11] throw(error(instantiation_error,_6914))
ERROR: [9] '$dicts':'.'(_6944,_6946,_6948) at /usr/local/lib/swipl-7.4.2/boot/dicts.pl:46
ERROR: [8] '<meta-call>'(user:(...,...)) <foreign>
ERROR: [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
因此,SWI 定义了另一个仿函数 [|]
来服务于 '.'
传统上所服务的目的,即列表仿函数.
SWI has, therefore, defined another functor, [|]
, to serve the purpose that '.'
traditionally served, which is the list functor.
4 ?- X is '[|]'(97, []).
X = 97.
5 ?- X = '[|]'(H, T).
X = [H|T].
6 ?-
奇怪的是,你可能会认为这会发生:
Oddly, you might think then that this would happen:
7 ?- write_canonical([H|T]).
'[|]'(_, _)
然而,发生的事情是这样的:
However, what happens is this:
7 ?- write_canonical([H|T]).
[_|_]
true.
所以对于 SWI Prolog,列表表示 [H|T]
已经是列表的规范表示.
So to SWI Prolog, the list representation [H|T]
is already the canonical representation of the list.
这篇关于如何使用 SWI-Prolog ./2 功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!