以下两个 Prolog 列表有什么区别? [英] What's the difference between following two Prolog lists?
问题描述
我对以下两个列表的差异感到困惑:
I'm kind of confused between the difference of the two following lists:
K = [1,2,3 | X].
和
K = [1,2,3,X].
布拉特科书中的问题其实是在conc
的上下文中.
The question in the book of Bratko is actually in the context of conc
.
浓度定义为:
conc([], L, L).
conc([X|L1], L2, [X|L3]) :- conc(L1, L2, L3).
现在的实际问题是是否在
The actual question now is if in
conc([1,2,3], [X], L2).
L2 与查询中的 K 是相同的列表 K = [1,2,3|X].
L2 is the same list as K in the query K = [1,2,3|X].
我不认为 L2 和 K 是一样的,但我不太确定如何解释.L2 是两个列表的串联.K 是我不确定的东西与可以由列表填充的变量 X 的串联...
I don't think L2 and K are the same, but I'm not so sure how to explain it. L2 is the concatenation of two lists. K is the concatenation of something I'm not sure of with a variable X that can be filled in by a list...
仍在学习 Prolog,如果这是一个愚蠢"的问题,请原谅我.
Still learning Prolog, so forgive me if this is a 'stupid' question.
推荐答案
在 Prolog 中,每个列表要么是空列表([]
),要么是头尾([A | X]
).Head 是第一个元素,tail 是列表的其余部分.
In Prolog each list is either empty list ([]
) or head and a tail ([A | X]
). Head is first element and tail is rest of the list.
示例:
[1]
实际上是[1 |[]]
[1, 2]
是[1 |[2 |[]]]
- <代码>[1,2,3 |[4, 5]] 与
[1,2,3,4,5]
相同(不同于 4 元素列表[1,2,3,[4,5]]
).
[1]
is actually[1 | []]
[1, 2]
is[1 | [2 | []]]
[1,2,3 | [4, 5]]
is the same as[1,2,3,4,5]
(which is different from 4-element list[1,2,3,[4,5]]
).
所以在你的例子中 L2
是 [1,2,3,X]
,它是最后一个元素 X
的四元素列表.同时 K
是 [1,2,3|X]
这是一个以 1,2,3
开头并且包含所有元素的列表X
.
So in your example L2
is [1,2,3,X]
, which is four element list with last element X
. Meanwhile K
is [1,2,3|X]
which is a lists which starts with 1,2,3
and than has all elements of X
.
下图展示了[1,2,3,X]
和[1,2,3|X]
的结构区别.
Following picture shows the difference between structures of [1,2,3,X]
and [1,2,3|X]
.
这篇关于以下两个 Prolog 列表有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!