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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

list that leads or comes after another list 1

Status
Not open for further replies.

xristiana

Programmer
Oct 23, 2010
48
GR
? '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.
 
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).
 
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.
 
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?
 
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?
 
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 :)
 
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!! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top