T.P
T.P

Reputation: 3

solve a simple append function using prolog language

when we make an append function in prolog, then I think this is the answer of that.

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

but I don't have the clear meaning of it.please give me that. what's the meaning of the character "L"? purpose??? (append([H|T],L,[H|R]):- append(T,L,R). give me a discribtion to this line)

Upvotes: 0

Views: 104

Answers (1)

Timothy Murphy
Timothy Murphy

Reputation: 1362

In general your append predicate takes 3 arguments (L1, L2, L3):

append(L1, L2, L3)

Append tells you if L3 is the list resulting from appending L1 to the front of L2.

For the First case:

append([],L,L)

You give the base case for append (i.e. what is the result of the empty list appended to some list, L. The result is just the original list, L).

Your second case tells how you can prove some list [H|T] appended to the front of another list L to create some other list [H|R] where H is the head of the first input list and the resulting list.

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

The second part of this rule says that this is only true if one can prove that the list R is the result of appending T (the tail of the original input list) to L.

Upvotes: 1

Related Questions