How does '|' work character in prolog app, not list splitting?

My example uses the append functor:

append([],L,L). 
append([H|T],L2,[H|L3])  :-  append(T,L2,L3).

//append([123],[abc],L3). this is the query

What really bothers me is the list [H | L3]. Shape what I read that takes off my head, as this adds it to the list. When it repeats through the list and starts coming back, how is it added? Thanks in advance.

+3
source share
3 answers

You can use it trace/0when you are wondering what the execution looks like.

Here is a quick overview.

  • You call append([1, 2, 3], [a, b, c], L3).
  • [1, 2, 3] not empty, second sentence applies
  • He calls append([2, 3], [a, b, c], L4).and remembers that the head L3is like a chapter [1, 2, 3]and that the tail L3is equalL4
  • [2, 3] not empty, second sentence applies
  • append([3], [a, b, c], L5). , L4 [2, 3] L4 L5
  • [3] ,
  • append([], [a, b, c], L6). , L5 [3] L5 L6
  • [] [],
  • , L6 [a, b, c]

, :

L6 = [a, b, c],
L5 = [3|L6],
L4 = [2|L5],
L3 = [1|L4].

Prolog L3 = [1, 2, 3, a, b, c].

, .

+2

[H|T] '.'(H, T). , . – . :

?- [1,2,3] = [X|L].
X = 1,
L = [2, 3].

, [1,2,3] = [X|L]. , , , . . , . , " ", . :

?- X = 1, L = [2, 3], M = [X|L].
X = 1,
L = [2, 3],
M = [1, 2, 3].

. :

?- M = [X|L].
M = [X|L].

. , 1 .

append/3, :

?- append(Xs, Ys, [X,Y,Z]).
?- append(Xs, Xs, Zs).
?- append(Xs, Ys, Zs)

.

+5

In Prolog, you never do anything as active as it takes off; it would be better to think of it as a "focus". (The prologue is declarative in nature, you do not say what to do, you say something similar.) For example, the list may be empty ( []) or non-empty ( [H|T]); Each non-empty list has a head Hand tail T, but |simply allows you to reference them. Unification can use a list declaration as a [H|T]split list or a list combining, if required.

+1
source

All Articles