测试 Prolog 差异列表
我一直在阅读有关列表差异有多大的文章,我希望测试书中的一些示例。但似乎您无法以与例如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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
Y 是两个谓词的答案。
Try:
Y is the answer for both predicates.
drotate
的定义可以简化:换句话说,简单地说,
现在,任何差异列表通常都写成一对,
AB
。因此,对drotate
的调用可能是drotate( [1,2,3|Z]-Z, RL)
,目的是查看RL
中的输出代码>变量。但是将此调用与最后一个定义相匹配,我们得到Z = [1|L]
,即 logvarZ
,在调用之前可能未实例化,但由它实例化,实际上在开放式列表[1,2,3|Z]-Z
的末尾添加1
,将其变成[1,2, 3,1|L]-L
。R
只是通过将[H|R]
与列表匹配来指向新扩大的列表的第二个元素。但也可以使用真正的循环数据来调用它,
AA = [1,2,3|Z]-Z, drotate( AZ, RL)
:The definition of
drotate
can be simplified:In other words, simply,
Now, any difference list is usually written out as a pair,
A-B
. So a call todrotate
might bedrotate( [1,2,3|Z]-Z, R-L)
with intent to see the output inR-L
variables. But matching this call with the last definition, we getZ = [1|L]
, i.e. the logvarZ
, presumably non-instantiated before the call, gets instantiated by it, actually adding1
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.But it could also be called with the truly circular data,
A-A = [1,2,3|Z]-Z, drotate( A-Z, R-L)
: