结构(差异列表)序言 [英] Structure (Difference Lists) Prolog

查看:39
本文介绍了结构(差异列表)序言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题参考了本书第3章的材料:使用 Prolog、Clocksin 和 Mellish 编程,第 5 版

This question refers to the material in chapter 3 of the book: Programming in Prolog, Clocksin and Mellish, Ed 5

本书第 72 页显示了一个使用差异列表的程序:

In page 72 of this book, a program using difference list is displayed:

partsOf(X,P):- partsacc(X,P,Hole) , Hole=[].

partsacc(X,[X|Hole],Hole):-basicpart(X).
partsacc(X,P,Hole):- assembly(X,Subparts), partsacclist(Subparts, P, Hole).

partsacclist([],Hole,Hole).
partsacclist([P|T], Total, Hole):- partsacc(P,Total,Hole1), partsacclist(T,Hole1,Hole).

在网上很多教程中,使用了下面的使用-"的格式,例如::

In many tutorials online, the following format of using the "-" is used, for example::

append([ A , B , C | R1 ] – R1 , [ D , E | R2 ] – R2 , R3)

我的问题是:

  1. 这两种表示有什么区别(使用 - 和不使用)

  1. What is the difference between these two representations (Using - and not using it)

在哪些情况下最好分别使用它们?

In which situations it is best to use each of them?

谢谢

推荐答案

无论如何:不要使用 (-)/2(\)/2 或任何其他运算符来表示差异列表".原因是您经常会有一个带有一个列表参数的谓词和一个使用差异列表的内部谓词.由于两者具有相同的数量并且可能还有相同的名称,事情会变得混乱.更糟糕的是,它可能适用于某些情况".此外,该运算符会产生一些成本,您可以使用两个单独的参数来避免.

By all means: Do not use (-)/2 or (\)/2 or any other operator to represent "difference lists". The reason is that you will often have a predicate with one list argument and an internal predicate that uses a difference list. With both having the same arity and probably also the same name, things will get confusing. Even worse, it might work for "some cases". Also, that operator will incur some cost you can avoid with two separate arguments.

尽量坚持干净的命名约定.即S0S1 ... S.通过这种方式,表示差异列表的参数将很容易看到.为了更好地强调这些参数属于一起,有些人不在分隔逗号后使用空格,而将它用于其他参数.因此:

Try to stick to a clean naming convention. That is S0, S1 ... S. In this manner the arguments representing the difference list will be easily visible. To better underline that those arguments belong together, some people do not use a space after the separating comma, whereas they use it for other arguments. Thus:

p(L+R, S0,S) :-
   p(L, S0,S1),
   p(R, S1,S).

此外,(-)/2 在 Prolog 中还有另一个含义.它用于表示一对 Key-Value,如 keysort/2.

Further, the (-)/2 has another meaning in Prolog. It is used to represent a pair Key-Value, as in keysort/2.

我所知道的任何建议差异列表运算符的 Prolog 书籍都来自 1980 年代.

Any Prolog book I know suggesting an operator for difference lists comes from the 1980s.

这篇关于结构(差异列表)序言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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