embraced list
embraced list
(OP)
Hi again. :(
I have other three programms, the first one is this: I have to make a programm, that the first list A, is embraced in the second list B. I tested two programms, but they were wrong. An example of the embraced list is, A=[5,6,7] and B=[0,3,5,6,7,9]. Thank you in advance.
I have other three programms, the first one is this: I have to make a programm, that the first list A, is embraced in the second list B. I tested two programms, but they were wrong. An example of the embraced list is, A=[5,6,7] and B=[0,3,5,6,7,9]. Thank you in advance.
RE: embraced list
If B starts with A, then this will be true:
CODE
A appended with something will give B.
If B ends with A, then this will be true:
CODE
Something appended with A will give B.
For A inside B, you can use a combination, but I think it's better that you try to find it yourself. You can always type queries at the Prolog prompt to see how different calls to append work
RE: embraced list
RE: embraced list
RE: embraced list
RE: embraced list
RE: embraced list
RE: embraced list
RE: embraced list
inside(A, B) :- append(A, _, B).
inside(A, B) :- append(_, A, B).
inside(A, B) :- append(_, A, C), append(C, _, B).
RE: embraced list
And then you need to switch places between those 2 appends from the 3rd rule.
You see, for one append to work properly you need to take care which of the parameters you send in with values and which you send in without values (unbound).
append(A, B, C)
Case 1: A, B, C all have values - append will work properly and will return true if A + B = C and false otherwise
Case 2: A, B have values, C doesn't - append will return in C the result of A + B
Case 3: A, B don't have values, C has a value - append will return in A and B all combinations such that A + B = C
Case 4: A and C have values - append will return in B the list that satisfies A + B = C
Case 5: B and C have values - similar to case 4, append will return in A the list that satisfies A + B = C
You can try all these at the Prolog prompt. For example, to test case 3, type:
?- append(A, B, [1, 2, 3]).
and press ';' after each solution
Now in you case, your first append is something like this:
append(_, A, C) - only A has a value
You are in neither of the 5 cases presented above. What can Prolog do with these arguments? What would you do if I gave you a list A = [1, 2] and told you to compute two other lists such that X + A = Y ... infinite solutions, right?
Switch places between those 2 appends and you should be fine.
RE: embraced list
RE: embraced list
RE: embraced list