Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

embraced list 1

Status
Not open for further replies.

xristiana

Programmer
Oct 23, 2010
48
GR
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.
 
Try using append like I suggested in the previous topic.

If B starts with A, then this will be true:

Code:
append(A, _, B)

A appended with something will give B.

If B ends with A, then this will be true:

Code:
append(_, A, B)

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
 
Please can you be more specific? I'm new to prolog, an these are very difficult for me. I have studied a tutorial,but it didn't help me much. Only the experience helps. For example, how am I going to put on paper the if conditions? And this combination is the most difficult part, help me please!
 
Is there any append/4?And if so, I haven't used it yet!
 
Ok, I 'm going to write the previous programm for the first two occasions. But what about inside? Can you help me more, at least how to start?
 
When I insert a correct example, it says true, but when I insert a wrong example, it stops working, it doesn't say false, what's going on?
 
Maybe you can present your code so that we can see what is good and what is wrong
 
Ok, here it is:
inside(A, B) :- append(A, _, B).
inside(A, B) :- append(_, A, B).
inside(A, B) :- append(_, A, C), append(C, _, B).
 
Ok, that's not bad ... now you need to erase the first two rules, because they describe frontier inclusion of A in B (either B starts with A, or B ends with A).

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.
 
Thank you very very much! I suppose we erase the first two rules because they are included in the third clause with the help of the void list. Correct me if I am wrong. I understood everything else, you always are a good explainer! :)
 
That's exactly right, the last rule includes the first two because the '_' symbol stands for anything, so also for a void list :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top