0
votes

I'm kind of confused between the difference of the two following lists:

K = [1,2,3 | X].

and

K = [1,2,3,X].

The question in the book of Bratko is actually in the context of conc.

conc is defined as:

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 is the same list as K in the query K = [1,2,3|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...

Still learning Prolog, so forgive me if this is a 'stupid' question.

1
conc([1,2,3],[X],[1,2,3,X]) and conc([1,2,3],X,[1,2,3|X]) both hold. That's actually very visually intuitive, especially the first one. - Will Ness

1 Answers

2
votes

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.

Examples:

  • [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]] ).

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.

Following picture shows the difference between structures of [1,2,3,X] and [1,2,3|X]. [1,2,3,X] vs [1,2,3|X]