Вопрос

Before this program I've seen in the following example that say if an element X belongs to a String L.

The code of this example is the following:

member2(X, [X|_]).
member2(X,[_|T]):- member2(X,T).

I have one fact that represent the base case (the X element belongs to the list if it is in the head of the list) and the rule is used to recursively search in the tail of the list if the element X is not the head...

This version was pretty obscure for me and someone advised me to see it in this way to understand how it work:

member2(X, [X|_]).
member2(X,Y):- Y = [_|T],
               member2(X,T).

So the meaning is more clear:

I have one fact that represent the base case (the X element belongs to the list if it is in the head of the list).

The rule say that I have to prove two things:

1) Y = [_|T] This is true because the anonymous variable _ unifies with anything

2) It recursively search the element X in the tail list

Ok, now I have a problem to understand this new exercise, I have to write a SWI Prolog program that, given two strings, concatenate the second string to the first in a third string

The solution is:

myappend([], L, L).
myappend([X|L1], L2, [X|L]) :- myappend(L1,L2,L).

I have some problem to understand how it work

I have understand that the fact represents the base case: when the first parameter is a void list [] and the second parameter is a not void list, the concatenation of the first parameter and second parameter is the second parameter list (and this equals to the third parameter)

The rule represents the general case in which the first parameter is not a void list...so I think that when this case occurs it delete the head of L1 parameter (the head of the first list) until this is an empty list [], so I will have the base case and the third parameter is unified to the second one...then do backtracking...

But I have problem to understand how exactly work the rule:

myappend([X|L1], L2, [X|L]) :- myappend(L1,L2,L).

I am trying to do something like the previous example and translate it in a more speaking logic form because in this way is very unclear for me....but I have not ideas how to do...how can I do

What is the logic behind this rule? what does it mean?

Это было полезно?

Решение

myappend([], L, L).

myappend([X|L1], L2, [X|L]) :- myappend(L1,L2,L).

An longer version of the 2nd rule:

myappend(L1, L2, Concat) :- 
    L1 = [HeadL1|RestL1], Concat = [HeadConcat|RestConcat], 
    HeadL1 = HeadConcat,
    myappend(RestL1,L2,RestConcat).

The first rule means that, if the first list is empty, the concatenated list is the same as the second list.

The second rule means that, the final list will be formed by the first element of the first list X, and a list L that is the concatenation of the rest of the first list L1 and the second list L2. Note that the first argument can only be unified only if it is a list and it is not empty, due to the constraint by [X|L1].

You can think of the predicate as testing whether the 1st, 2nd and 3rd argument has the relation of 1st and 2nd concatenates is the 3rd argument. Since it is easy and efficient to extract head element of a list in Prolog, the solution above picks out the first element, match them ([X|L1] and [X|L]) and test that the rest of the elements satisfy the concatenation relation.

Другие советы

Concatenation([ ],L, L).

Concatenation ( [X | L1 ], L2, [ X | L ] ):- Concatenation ( L1, L2 , L).

Here we are adding element/list at the end of the existing list.

Concatenate(existing list, element, [existing list| element])

1 -> if the list is empty, concatenated list will be second list. For second statement take example: Existing list = [1,2]
Adding element 3

Concatenate([X|L1], L2, [X|L]):- Concatenate (L1, L2, L).

Concatenate ([1|2], 3, [1|x]):-

Concatenate ([2],3,[2|y]):- (x= [2|y];)

Concatenate ([], 3, 3). (y = 3;)

Therefore the final list becomes [1,2,3].

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top