Paste the item into the list and return the same list, updated

Hi, I am trying to insert an item into a list, but it is very important from my program that the result be saved in the original list, and not in the new one. Any code that I wrote or found on the Internet succeeds only when creating a new list in which the final result is saved.
So my question is: can someone tell me how to define a function: insert (X, L), where X is an element and L is a list?

+6
prolog
source share
3 answers

No, Prolog just doesn't work. There is no such thing as a β€œchange" of meaning. A variable can be unified with a specific value, but if it was already [1,3] , it will never be [1,2,3] later.

+3
source share

According to the interlocutor, you cannot add or make any changes to the correct list, i.e. A list in which each item is already linked. The only β€œchange” we can make is to combine one expression with another.

However, there is the concept of a partial list to which additional elements can be added at the end. This is usually called a list of differences, although this nomenclature may not be immediately understood.

Suppose we start not with an empty list, but with a free variable X. However, we can think about subtracting X from X and getting "nothing." That is, an empty list of differences is presented by X - X. The minus "-" here is a purely formal operator; no difference estimate is required. This is just a handy syntax that you see from how difference lists can be used to accomplish what you (possibly) want to do.

We can add an item to the list of differences as follows:

 insertDL(M,XY,XZ) :- Y = [M|Z]. 

Here M is the new item we want to add, XY is the "old" list of differences, and XZ is the "new" difference (to which M is added, by combining the previously free variable Y with the partial list [M | Z], so Z becomes the "open" tail of the partial list X).

When we finally insert things into our list of differences, we can turn X into the correct list by setting the "free tail" at this point to an empty list []. In this sense, X is the "same" variable as it was at the first start, simply unified by step-by-step steps from a free variable to the corresponding list.

This is a very powerful method in programming Prolog, and some practice is required for its convenience. Some links to further discussion on the Internet:

[From Prolog lists to difference lists]
http://www.irisa.fr/prive/ridoux/ICLP91/node8.html

[Implementation of difference lists in Prolog]
http://www.cl.cam.ac.uk/~jpw48/difflists.pdf

[Lecture notes: Lists of differences]
http://www.cs.cmu.edu/~fp/courses/lp/lectures/11-diff.pdf

+2
source share

Some prologs provide the setarg / 3 predicate to change conditions in place.

To use it on lists, you only need to consider that they are just a good representation of the chains of compound members with the functor '.'/2

In any case, when you need to use setarg / 3 in Prolog, this probably means that you are doing something wrong.

+1
source share

All Articles