Updated April 6, 2023
Introduction to Prolog append
Prolog append is defined as the operation which adds items of one list into another by using prolog programming language, it is a basic operation on prolog. prolog is a logical and declarative programming language, it is an important predicate in prolog which is defined by append/3(List3 to the result of appending List2 to List1)whose arguments are all lists. the lists in append have declared as ‘append(L1, L2, L3)’ it happens when the List L3 is the result of concatenating the lists L1 and L2 together, it is also true that appending the empty list to any list whatever yields that the same list.
append([ ], Y, Y). append([X|L1],L2,[X|L3]):-append(L1,L2,L3).
- append(, Y, Y). – append , Y to get Y
- append ([X|L1],L2,[X|L3]). – append[X|L1] and Y to get [X|L3]
- append(L1,L2,L3). – If append X and Y to get Z
Above is the declarative syntax for append in prolog, after performing operations on that the results will be in the format of ‘append (L1, L2, L3)’, in which it holds L3 is the resulting list which is get by concatenating L1 and L2 together, where the list is the combination of items and append is the relation between the lists, the list is either empty or it is a combined with head and tails, the empty list is represented by .
How does append work in Prolog?
The append works on the list in prolog, which means that append working on combining two lists or joining two lists together, for example, if we have two lists and we have to combine that into one list then append has that syntax to join two that lists together, we can also say that append is a relation between lists. Appending two lists together or adding one list as an item. Now if in case the items are already present in the list then append will not work for that we need to create one predicate which name is ‘list_append(L1, L2, L3).’, let us take look into it, let P is an element, L1 is a list then if L1 has P element already in it then L1 will be.
To illustrate the working of append in prolog, we will see it by taking examples as below, we will have to define an important predicate append/3 whose arguments are all lists. Let us declare append[L1, L2, L3], it holds the list L3, where L3 is the resultant list which gets after joining L1 and L2 lists if we pass the query ‘append([p, q, r],[1, 2, 3],[p, q, r, 1, 2, 3])’ then at first we get the response, yes, and then if we pass the query ‘append([p, q, r],[1,2,3],[p, q, r, 1, 2])’ then we will get the response no.
So, now as per the procedural perspective, the append/3 is used to concatenate two lists together, we can do it by using one variable as a third argument, then present the query
‘append([p, q, r],[1, 2, 3], L3).’ by performing this query the response will get as ‘L3 = [p, q, r, 1, 2, 3]yes, and append/3 is also used to split the list.
Let we will see the defining of append/3 now:
append(, L,L). append([H|T],L2,[H|L3]):-append(T,L2,L3).
This type of declaration is called a recursive declaration in a prolog, appending any empty list will resulting in a list. First, we have to concatenate a non-empty list [H|T] with list L2, this will end up with the list whose head is H and tail is the result get after concatenating T with L2, for that the input ‘[H| T]+L2’ which will give the result as ‘[H|L3]’.
Now, we will see the details about appending, by using search tree patterns to understand how appending works with non-empty lists and empty lists. Suppose we want append lists [9, 2, 6, 8, 10] and [5, 1, 3]. While appending first it will check that the list in which we are appending is empty.
So we want to append [9, 2, 6, 8, 10] to [5, 1, 3], the list being appended is non-empty then the result has 5 as a first element which is followed by the result of appending [9,2,6,8,10] to [1,3].
Now, we have to append [9,2,6,8,10] to [1,3], the list being appended is not empty then 1 as the first element in a result which is followed by the appending [9,1,6,8,10] to .
Again to append [9,2,6,8,10] to , again the list being appended is non-empty, take 3 as a first element followed by the result of appending [9,2,6,8,10] to , now the list being appended is empty so the resultant list will be [9,2,6,8,10].
The result of appending [9,2,6,8,10] to [ ] is [9,2,6,8,10] hence the result of appending [9,2,6,8,10] to [ 3] is [3,9,2,6,8,10], again appending lists [3,9,2,6,8,10] to [1,3] result is [1,3,5,9,2,6,8,10].
Since the result of appending [9,2,6,8,10] to [5,1,3] is [5,1,3,9,2,6,8,10] and the result of appending [9,2,6,8,10] to [5,1,3] is [5,1,3,9,2,6,8,10].
Note: In this article, we did not use the prefix ‘?-’ while giving input because the compiler which is used here is not allowed to use it.
Examples of Prolog append
Here are the following examples mention below
:- initialization(main). main :- write('append'). append( , X, X). append( [X | Y], Z, [X | W]) :- append( Y, Z, W). Input: append([1,2],[3,4,5], X).
In the above example of by using append, the code has been written, by performing steps append the third list from two lists, where we can say if X=[1, 2], Y=[3, 4, 5], and Z is the joining of X and Y which is the third list.
:- initialization(main). main :- write('append clauses'). append(List1, List2, Result) :- List1 = [Head1 | Tail1], Result = [HeadR | TailR], Head1 = HeadR, append(Tail1, List2, TailR).
append([9, 2, 3, 4], [10, -5, 6, 7, 8], R).
In the above example, we took an example of the clauses in code format, for that we perform the operation in the format of head and tail, then internally step by step the number can indicate the clause which is used for each time and we gave input to the list so that the resultant list is seen in the screenshot.
In the above article, we conclude that in the prolog the features of append predicate are in-built, the append predicate is used for appending the elements into the list and also for calling the list to append, append/3 is used to split the list into two-part, also we conclude that append is the relation between two lists.
We hope that this EDUCBA information on “Prolog append” was beneficial to you. You can view EDUCBA’s recommended articles for more information.