×
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

list that leads or comes after another list

list that leads or comes after another list

list that leads or comes after another list

(OP)
Ι 'm writing this again, because noone noticed, probably because I had one reply by myself, because I had made a mistake. So, if you haven't seen, it, please answer me my questions. Thanks.

"Hello everyone,I have to write two programms with lists, with two arguments. In the first programm the first argument (list) must lead the second (list) and in the second programm the opposite. I don't understand the theory very well, when I have news I'll inform you, unless you understand it first, but I made a programm which I don't know what exactly does. It's the following:
leading_list([],Y).
leading_list(Head|X1,Head|Y1):-leading_list(X1,Y1).
I thought I had understood what leading means, but now I know I haven't.Can anyone tell me what this programm does, and give me an idea how the programm for example (programm)1 should be? Unless you don't understand it and I will explain it to you when I find out. Thanks."

RE: list that leads or comes after another list

Maybe you should put the last rule like this:

CODE

leading_list([Head | X1], [Head | Y1]) :-
    leading_list(X1, Y1).

It basically says (read it backwards) that if X1 is a list that is the leading list for another list Y1, then if you put any element (Head) in front of X1, you will get a list that is the leading list for [Head | Y1]

Example: if [2, 3, 4] is the leading list for [2, 3, 4, 5, 6], then [1, 2, 3, 4] is the leading list for [1, 2, 3, 4, 5, 6]. And if you replace 1 with anything else, it would still do the trick.

So the final version of the code would be:

CODE

leading_list([], _).
leading_list([Head | X1], [Head | Y1]) :-
    leading_list(X1, Y1).

RE: list that leads or comes after another list

(OP)
Thank you very much Mr. Kahleen. I had forgotten the []. I still don't understand what leads, or comes after mean. You say that if we replace 1 with anything else, it would be the same. You mean if we had [1,2,3,4] as the leading list for [2,3,4,5,6] would be the same as if the [1,2,3,4] was the leading list for [1,2,3,4,5,6]? And also, I have an example of the second programm, the one list that comes after another, and it says, that [1,2], comes after [2]. Thank you very much.

RE: list that leads or comes after another list

"A is leading B" means that list B starts with list A, and eventually has other elements following. So [1, 2, 3] starts with [], and with [1] and with [1, 2], but does not start with [4, 5]. It's really simple.

What I meant with "anything else" is somewhat different. Take a look at your main rule:

CODE

leading_list([Head | X1], [Head | Y1]) :-
    leading_list(X1, Y1).

... and now read it backwards ... in forward read, ':-' means "if" ... in backward read, ':-' means "implies" ... and leading_list(A, B) means "A is leading B"

leading_list(X1, Y1) -> leading_list([Head | X1], [Head | Y1]).

Now translate it into English:

X1 is leading Y1 implies [Head | X1] is leading [Head | Y1]

From this rule, you can see there is nothing restricting the variable Head. So if a list X1 is leading another list Y1, then if you put any element Head in front of X1 and in front of Y1 as well, the resulting lists will also obey the same leading rule: [Head | X1] will lead [Head | Y1]. Head can be really anything, like 1, 2, 9 or even mike.

[1, 2, 3] is leading [1, 2, 3, 5, 7, 9] implies [mike, 1, 2, 3] is leading [mike, 1, 2, 3, 5, 7, 9]

See the resemblance with your rule?

RE: list that leads or comes after another list

(OP)
Ok, now I understand your point,and thanks,but I still don't understand when a list follows another. How does [1,2] come after [2]? I have been confused. And how can I solve this programm, if I change the first rule by swapping the two arguments, or by putting out the "head" from the second rule and comparing the rest of the two lists?

RE: list that leads or comes after another list

My understanding is that [2] comes after [1, 2], not the other way around.

If "A is leading B" means "list B starts with list A"
then "A comes after B" should mean "list B ends with list A".

That's how I see things if there is no clear definition of the terms "leading" and "coming-after"

You can't use the approach from the "leading" program. When testing that a list B starts with a list A we test that A starts with Head and B starts with the same Head, we remove the Head from both A and B and continue until list A becomes void. This process will not happen if list A is at the end of list B.

You need something like this:

CODE

ending_list(A, A).
ending_list(A, [_ | B]) :-
    ending_list(A, B).

First clause: list A is at the end of list A (that's obvious).

Second clause: if list A is at the end of some list B, then the same list A is at the end of list [_ | B] (the same list B with any element in front of it).


Now both your problems could very easily be solved like this:

CODE

at_the_beginning(A, B) :- append(A, _, B).
at_the_end(A, B) :- append(_, A, B).

I believe further explanations are futile on the last code :)

RE: list that leads or comes after another list

(OP)
Thank you very very much, I understood all, I have other three programms to make, when I have questions I'll write them down. Thank you!! :)

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