English 中文(简体)
博客清单: com马和|子组合
原标题:Prolog lists: combination of comma and |

In Prolog, [a, b, c] = [a, b, c | []]

However, I am confused about what the head of [a, b, c | []] is? If [a, b, c] = [a, b, c | []] why is it not true that: [a, b, c] = [a, b, c | []] = [[a, b, c | []] | []]] = ... (so I added another empty list at the end of the head, since they are elements separated by a comma, just like it is the case with [a, b, c] where we also added an empty list)

So I see a comma as: combine all the elements via the | operator, and add an empty list at the end. "a, b, c" (the head of [a, b, c | []]) would then become [a, b, c | []] again and so on.

但是,我的解释是错误的,因为我刚才所说的话是错误的。

And If I m not mistaken, | has a higher precedence than the comma operator. So [A, B|C] = [A|[B|C]]. But why is it not the case that [A, B|C] = [A|[[B|C]|[]]].

I can memorize the correct definition as follows: combine the elements via |, and add an empty list at the end only if the last operand of the commas is enclosed by a ]. So then: [a, b, c] = [a, b, c | []], and if I got [a, b, c | []], even though I got commas here, I wouldn t add an empty list since the last element (c) is not enclosed by a ]. But this seems to be not a good way to understand this.

最佳回答

缩略语:[] > 列出了一份名单,[]

?- write_canonical([a,b,c|[]]).
.(a,.(b,.(c,[]))).

?- write_canonical([[a,b,c|[]]|[]]).
.(.(a,.(b,.(c,[]))),[]).

它们彼此之间可打上<条码>=,因为它们结构不同。 确实如此。


令我困惑的是,[a、b、c、|]的负责人是什么?

The head of a list is the first item, that s a list of three items and the first one is a:

?- [Head|Tail] = [a,b,c|[]].
Head = a,
Tail = [b, c].

[a、b、c、[ ]] = [[a、b、c、[]][>]

如Evgeny回答,[、b、c、[]、>、>>>>。

因此,我看到一个ma子:把所有要素都通过“非洲红新月”来合并,最后添加一个空洞的清单。 “a、b、c”([a、b、c、])之后将再次成为[a、b、c、]。

我看到一份清单,即把清单中的项目分开,把管道连接到最后另一个清单。 最后列出一个空洞的清单,只字不提目的,但没有必要写上,因为只有<代码>[a,b,c]<>/代码”就足够清楚。


答复评论:

1: Sometimes the comma operator adds an empty list to the end, sometimes it doesn’t.

我刚刚走了最后的 com子,并加上了一个空洞的清单和一个管道。

我不理解对 com的这种关注;你增加了一条管道和一个空洞的清单,你在另一层清单中总结了全部内容。 页: 1

页: 1 而是a b c-> (a b c [])-> (a b c []]) 不同。

<代码>[a,b,c]为清单。 接着,<代码>[a,b,c.Rest]是一份无地尾附着的清单(“必须用空号终止清单”)是假的,这份清单没有固定长度。 然后,[a,b,c.Rest],Res=[1,2,3],编号至信函结尾。 然后,[a,b,c.Rest],Res=[>, 单列,即 抗议者知道一份名单已经结束,因此现在这份固定期限清单。 3. 。 如果你想要的话,可以写到<代码>[a,b,> >>,但我不知道,为什么你希望照同<代码>[a,b,c],而且这种编号缩短,更容易书写。

不能写上<代码>[a、c、[>、]<>>>、>/代码和<代码>。 我仍然认为外部<代码>[ ]是清单编制者,超过了您对 com的重视程度,这就是为什么我这样说。

什么时候加上了空名单?

当你想要的时候。 很想写一下它,你也不必这样做。

就我的第一个问题而言,提及我的问题可能是有益的,因为我们可以用管道取代 com。

但是,我们增加了一个隐藏的空洞清单。

如果你想要的话。 这份固定收入清单的结尾。 在幕后,你不一定总是需要引起注意。 <代码>[a,b,c]。 这份清单与一份空档清单的清单不相同,最后一项是“随附一份空档清单”,即<代码>。

问题回答

这是一个非常有趣的问题。

Prolog模型将第一要素与通过[><>>>>>>>>1--rest concatenation的其余部分合并起来。

就业清单是字面上的。

根据定义,非豁免清单以空洞清单为目的。

考虑这一问题:

EmptyList = [],
SingleElementList = [e],
TwoElementList = [a, b],
EmptyList =.. EmptyListStructure,
SingleElementList =.. SingleElementListStructure,
TwoElementList =.. TwoElementListStructure.

抗议:

EmptyList = [],
EmptyListStructure = [[]],
SingleElementList = [e],
SingleElementListStructure = [ [|] , e, []],
TwoElementList = [a, b],
TwoElementListStructure = [ [|] , a, [b]]

运营商不是互换的,不是联系的,也不是需求。 因此,请参见<代码[a、b、c]、<>>>>。

At least SWI-prolog actually allows you to use [|] operator directly, so you can construct a two element list as

TwoElementList =  [|] (a,  [|] (b, [])).

结果是,从<代码>[a, b]中无法区分的,因为这实际上是这样。

我知道这并不是立即进行的,而是至少是自信的。 希望这一帮助。





相关问题
Finding a class within list

I have a class (Node) which has a property of SubNodes which is a List of the Node class I have a list of Nodes (of which each Node may or may not have a list of SubNodes within itself) I need to be ...

How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

Is List<> better than DataSet for UI Layer in ASP.Net?

I want to get data from my data access layer into my business layer, then prepare it for use in my UI. So i wonder: is it better to read my data by DataReader and use it to fill a List<BLClasses&...

What is the benefit to using List<T> over IEnumerable<T>?

or the other way around? I use generic lists all the time. But I hear occasionally about IEnumerables, too, and I honestly have no clue (today) what they are for and why I should use them. So, at ...

灵活性:在滚动之前显示错误的清单

我有一份清单,在你滚动之前没有显示任何物品,然后这些物品就显示。 是否有任何人知道如何解决这一问题? 我尝试了叫人名单。

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签