Trending September 2023 # Learn How Does Append Work In Prolog # Suggested October 2023 # Top 18 Popular |

Trending September 2023 # Learn How Does Append Work In Prolog # Suggested October 2023 # Top 18 Popular

You are reading the article Learn How Does Append Work In Prolog updated in September 2023 on the website We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Learn How Does Append Work In Prolog

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

append([ ], Y, Y).


append([], Y, Y). – append [], Y to get Y

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).

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].

Again to append [9,2,6,8,10] to [3], 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

Example #1


:- initialization(main). main :- write('append'). append( [], X, X). 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.

Example #2


:- initialization(main). main :- write('append clauses'). append(List1, List2, Result) :- 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.

Recommended Articles

We hope that this EDUCBA information on “Prolog append” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

You're reading Learn How Does Append Work In Prolog

Update the detailed information about Learn How Does Append Work In Prolog on the website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!