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."
"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
CODE
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([Head | X1], [Head | Y1]) :-
leading_list(X1, Y1).
RE: list that leads or comes after another list
RE: list that leads or comes after another list
What I meant with "anything else" is somewhat different. Take a look at your main rule:
CODE
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
RE: list that leads or comes after another list
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, [_ | 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_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