×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

embraced list

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.  

RE: embraced list

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

RE: embraced list

(OP)
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!

RE: embraced list

(OP)
Is there any append/4?And if so, I haven't used it yet!

RE: embraced list

(OP)
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?

RE: embraced list

(OP)
Ok, ok I think I found it thanks :)

RE: embraced list

(OP)
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?

RE: embraced list

Maybe you can present your code so that we can see what is good and what is wrong

RE: embraced list

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

RE: embraced list

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.

RE: embraced list

(OP)
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! :)

RE: embraced list

That's exactly right, the last rule includes the first two because the '_' symbol stands for anything, so also for a void list :)

RE: embraced list

(OP)
Thanks:)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close