测试 Prolog 差异列表

发布于 2024-11-19 00:38:16 字数 338 浏览 2 评论 0原文

我一直在阅读有关列表差异有多大的文章,我希望测试书中的一些示例。但似乎您无法以与例如append([1,2,3],[4,5],X)相同的方式传递列表作为输入,其中X=[1,2,3, 4,5]。奇怪的是,我查阅过的书都没有提到这一点。

我正在 swipl 上运行代码,我有兴趣测试差异附加谓词:

dapp(A-B,B-C,A-C).

和“旋转列表的第一个元素”谓词:

drotate([H|T]-T1,R-S) :- dapp(T-T1,[H|L]-L,R-S).

任何想法,如何在 swipl 中测试这些谓词?

I've been reading about how great difference lists are and I was hoping to test some examples from the books. But it seems that you can't pass lists as input in just the same way as, for instance append([1,2,3], [4,5], X), where X=[1,2,3,4,5]. Strangely, no book I've consulted ever mentions this.

I'm running the code on swipl and I'm interested in testing out a difference append predicate:

dapp(A-B,B-C,A-C).

and a "rotate first element of list" predicate:

drotate([H|T]-T1,R-S) :- dapp(T-T1,[H|L]-L,R-S).

Any ideas, how I can test these predicates in swipl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜点 2024-11-26 00:38:16

尝试:

dapp([1,2,3|X] - X,[4,5,6] - [],Y - []).
drotate([1,2,3|X] - X,Y - []).

Y 是两个谓词的答案。

Try:

dapp([1,2,3|X] - X,[4,5,6] - [],Y - []).
drotate([1,2,3|X] - X,Y - []).

Y is the answer for both predicates.

红玫瑰 2024-11-26 00:38:16

drotate 的定义可以简化:

dapp( A-B, B-C, A-C). 
drotate( [H|T]-T1, R-S) :- % (* dapp( T-T1, [H|L]-L, R-S). 
       %%                       dapp( A-B , B    -C, A-C). 
       %% use the definition of dapp: *)
                             T = R, T1 = [H|L], L = S. 

换句话说,简单地说,

drotate( [H|R]-[H|L], R-L).

现在,任何差异列表通常都写成一对,AB。因此,对 drotate 的调用可能是 drotate( [1,2,3|Z]-Z, RL) ,目的是查看 RL 中的输出代码>变量。但是将此调用与最后一个定义相匹配,我们得到 Z = [1|L],即 logvar Z,在调用之前可能未实例化,但由它实例化,实际上在开放式列表 [1,2,3|Z]-Z 的末尾添加 1,将其变成 [1,2, 3,1|L]-LR 只是通过将 [H|R] 与列表匹配来指向新扩大的列表的第二个元素。

?- drotate( [1,2,3|Z]-Z, R-L).

Z = [1|_G345]
R = [2, 3, 1|_G345]
L = _G345 

Yes

但也可以使用真正的循环数据来调用它,AA = [1,2,3|Z]-Z, drotate( AZ, RL)

?- A-A = [1,2,3|Z]-Z, drotate( A-Z, R-L).

A = [1, 2, 3, 1, 2, 3, 1, 2, 3|...]
Z = [1, 2, 3, 1, 2, 3, 1, 2, 3|...]
R = [2, 3, 1, 2, 3, 1, 2, 3, 1|...]
L = [2, 3, 1, 2, 3, 1, 2, 3, 1|...] 

Yes

The definition of drotate can be simplified:

dapp( A-B, B-C, A-C). 
drotate( [H|T]-T1, R-S) :- % (* dapp( T-T1, [H|L]-L, R-S). 
       %%                       dapp( A-B , B    -C, A-C). 
       %% use the definition of dapp: *)
                             T = R, T1 = [H|L], L = S. 

In other words, simply,

drotate( [H|R]-[H|L], R-L).

Now, any difference list is usually written out as a pair, A-B. So a call to drotate might be drotate( [1,2,3|Z]-Z, R-L) with intent to see the output in R-L variables. But matching this call with the last definition, we get Z = [1|L], i.e. the logvar Z, presumably non-instantiated before the call, gets instantiated by it, actually adding 1 at the end of the open-ended list [1,2,3|Z]-Z, turning it into [1,2,3,1|L]-L. R just gets pointed at the 2nd element of the newly enlarged list by matching [H|R] with the list.

?- drotate( [1,2,3|Z]-Z, R-L).

Z = [1|_G345]
R = [2, 3, 1|_G345]
L = _G345 

Yes

But it could also be called with the truly circular data, A-A = [1,2,3|Z]-Z, drotate( A-Z, R-L):

?- A-A = [1,2,3|Z]-Z, drotate( A-Z, R-L).

A = [1, 2, 3, 1, 2, 3, 1, 2, 3|...]
Z = [1, 2, 3, 1, 2, 3, 1, 2, 3|...]
R = [2, 3, 1, 2, 3, 1, 2, 3, 1|...]
L = [2, 3, 1, 2, 3, 1, 2, 3, 1|...] 

Yes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文