I'm new to Prolog. Consider the following example:
MainList = [["A","a","0"],["A","b","0"],["B","a","0"],["B","b","0"],["A","a","1"],["B","b","1"],["C","c","1"],["C","a","0"]]
SubList = ["A","b","-']
The format of MainList: in the first index it should be the big letter, second index should be small letter and third index should be number. MainList can contain other formats (and lengths) but I choose this format because it is easier to explain. I just want to explain that there index of each sublists are divided into categories - first element of each list belongs to category1, second element to category2 and so on. The "-" symbol means we does know about the bond of that element with the other elements.
The relation I would like to create should go through the MainList and remove all of the sublists that does not follow SubList. For the example above, it should return:
Output = [["A","b","0"],["B","a","0"],["C","c","1"],["C","a","0"]]
Explanation: "A" is working together with "b" so every sublists that contains one of those elements, should be checked. if they both exists then it's ok (without duplicates). Otherwise if only one of them exists, it is not ok and we should not insert it into the Output list.
Another example (MainList is a bit different than the one before):
MainList = [["A","a","0"],["A","b","0"],["B","a","0"],["B","b","0"],["A","b","1"],["B","b","1"],["C","c","1"],["C","a","0"]]
SubList = ["C","a","0"]
Output = [["A","b","1"],["B","b","1"],["C","a","0"]]
I do understand the algorithm - we should go through each sublist of the MainList and check if the connection of the input-sublist is working, if so, we will insert it into the Output list. The problem is, I don't understand how to implement it right. What if the sublist contains more than 2 connections (as was shown in the second example)? How should I treat differently to the sublist?
EDIT: I'll try to explain a bit more. The place of each element is important. Each place represents a different category. For example, the first place might represent the big letters, the second place represents the small letters and the third place represents numbers (there could be more categories).
We get a sublist which represents a bond between two or more elements. For example we have a bond between "A" and "b": ["A","b","-"]. We should iterate through the sublists of MainList and check if each one of those sublists contains one of those elements ("A" and "b"). If it does, we should check if the bond is correct. For example if we have a sublist that has A (in the first index of course), then we should go to the second index and check if "b" is there. if it is not there, we should not insert it into the Output list, Otherwise we will insert. For example we have a bond ["A","b","-"] and we got into a sublists of MainList which looks as following: ["A","a","0"] or ["B","b","2"]. We will not inert those lists into Output. But if we got to sublists like: ["A","b","1"] and ["A","b","2"] and ["B","a","1"] we will insert those lists into Output.
I wrote a code that satisfies the output conditions for the examples you have given, but did not try other cases or consider the efficiency of the code, so you can maybe improve upon it. Here is the code :
subList(List,SubL,Out):-
(member("-",SubL),
select("-",SubL,SubRem)
;
\+ member("-",SubL),
SubRem = SubL),
findall(L,((member(L,List),checkEq(SubRem,L));
(member(L,List),checkNeq(SubRem,L))),Out).
checkEq([],_).
checkEq([S|Rest],[E|List]):-
S == E,
checkEq(Rest,List).
checkNeq([],_).
checkNeq([S|Rest],List) :-
\+ member(S,List),
checkNeq(Rest,List).
Explanation: What I did is that, first I removed the "-" character from the subarray(if it exists) in order to make the computations easier. Then, I either check the condition that each element in the SubList is in order with the elements of the selected sublist of the MainList. If this fails, I then check if none of the elements of SubList is contained in the selected sublist. If both checks fail, I move to the next sublist. Using findall/3 predicate, I find all combinations that satisfy either of these conditions and group them in the list Out
EDIT: Add the additional clause for the checkEq predicate :
checkEq([S|Rest],[E|List]):-
S \= E,
member(S,List),
checkEq([S|Rest],List).
So the final version is :
checkEq([],_).
checkEq([S|Rest],[E|List]):-
S == E,
checkEq(Rest,List).
checkEq([S|Rest],[E|List]):-
S \= E,
member(S,List),
checkEq([S|Rest],List).
Related
I am trying to figure out how to do this little thing. I have 2 lists for example [1,1,2,3,4] and [2,1,4,3,1], I need to confirm if all elements from list 1 are included in list 2, soo if i give the above lists as input this should be true, but if its like this [1,1,2,3,4] and [2,1,4,3,1,1] (three 1's) it should give false, this has to be done without using sort function.
I assume you know how to write a list as head and tail ([H|L]).
So you could use the predicate member/2 to ask for every element from the first list to be in the second list as well, but this would not solve the issue of duplicates. Using the predicate length/2 will not help in this case. So you need something that retracts one matching element from a list. You can either write your own find-and-remove-predicate or use the predicate append/3 to do so. append/3 is thought to append 2 lists to form a third one, but it can also be used to divide one list into two. If you state that your element as the head element of the second divided list you basically get a 'remove element' functionality. Once you've got the 2 divided lists, combine them into a new list and call the predicate again, but this time without the head element from list one and with the reappended-list. So in each step you remove one element from each list until you finally hit two empty lists (permut([],[]).). If something other than this two cases should appear, then the two lists are not permuations of each other and the predicate fails.
Since there is no use in backtracking other positions I inserted a cut (!) after successfully finding an element in the second list. The predicate works without the cut as well.
permut([],[]).
permut([H|T], Compare):-
append(C1, [H|C2], Compare),
!,
append(C1, C2, Cnext),
permut(T, Cnext).
gives the output
?- permut([1,2,3,4,5],[5,4,3,2,1]).
true.
?- permut([1,2,3,4,5],[5,4,3,2,1,1]).
false.
?- permut([1,2,3,4,5,6],[5,4,3,2,1]).
false.
I have a list of list and a list. I want to update the list of list by using the element from the second list.
For example:
I have a list of list
[[banan,NA],[apple,NA]] and a list [sweet,notsweet],
want to update the list of the list, so I will have a list of list
[[banana,sweet],[apple,notsweet]]
I have tried the code below, but I think I cannot figured out the base case correctly.
update([[]],[],[]).
update([[T|_]|HH],[FB|H2],[NState|_]) :-
NState=[T|FB],
update(HH,H2,NState).
Any help, will be much appreciate
thanks
You're almost there. First let's observe that the first list is empty if there are no more [fruit,*] pairs left, hence the first argument of your base case should be []. At that point the other lists have to be empty too, since they are of the same length.
In general, the first list will have a two-element list as its head, the first of which is being your object of interest and the second of which you don't care for, that is, something like [X,_]. The tail of that list will contain further X's, so let's maybe call it Xs. Then the first argument looks like [[X,_]|Xs]. The second argument is a flat list, so you can write [Y|Ys] (read as: the list starts with a Y that is followed by further Y's). The last argument is a two-element list [X,Y] that is followed by other such pairs (XY's), hence: [[X,Y]|XYs]. The relation has to hold for the tails as well, that can be described by a recursive goal. You can express the above in Prolog like so:
update([],[],[]).
update([[X,_]|Xs],[Y|Ys],[[X,Y]|XYs]) :-
update(Xs,Ys,XYs).
With these alterations to your predicate the example query from your comment yields the desired answer:
?- update([[banana,*],[apple,*]],[sweet,notsweet],C).
C = [[banana, sweet], [apple, notsweet]].
Given a list (A) I want to be able to create a new list (B) that contains only the elements of A that are the smallest or the biggest compared to their next and previous element. My problem is that I don't know how to do the comparisons of each element with its previous one.
(This question may be silly but I'm new to prolog and any help would be appreciated.)
You could start with something like that:
compareElem([]).
compareElem([H,H1,H2|B]):-compareElem(B),
compare(?Order, H1,H2),
compare(?Order, H1, H).
where ?Order is the order of comparison (like '<' or '>'). See compare/3.
Some queries:
?- compareElem([1,2,3,4,5,6]).
true.
?- compareElem([1,2,3,4,5,3]).
false.
of course to apply this example you must ensure that the list has 3n elements, this is just a basic example. Together with this comparison you can generate the other list
I need to write a list that must contain more than 3 elements if the elements are not numbers and if the elements are numbers, it must contain exactly 3 elements and the third element must be the result of the addition of first and second element. This is what I have:
mylist([W, X,Y,Z|_]). % List contains at least 3 Elements
mylist([X,Y,Z]):- Z is X+Y. % if there are 3 numbers, third number is the addition of first and second
Problem is that this is too general and works only partially. It will match mylist(3,3,3) because of the first fact though it should not, since this is a numbers' list and it should say no. But I don't know how I can differentiate between a numbers' list and a letters' list
You could use number/1 which succeeds when the argument is a number:
mylist([_, _,_,_|_]).
mylist([X,Y,Z]):- number(X),number(Y),number(Z),Z =:=X+Y.
Now mylist([3,3,3]) fails:
?- mylist([3,3,3]).
false.
I have a list of substitutions between variables and values
they are presented in a list [[x,y],[1,2]] (meaning that the value of x equals 1, and the value of y equals 2).
I want to change the list to a list of pairs meaning [[x,1],[y,2]],
I tried to use append so that I will create a pair in each step of the recursion and append it to a new list but i have problem of doing so (mainly selecting the head and tails of each pair)
your example it's a bit contrived, but transpose it's a bit overkill, specially if you're going to implement yourself. I think this should solve your need
'change an order of list of lists'([X_Y_Z, A_B_C], XA_YB_ZC) :-
findall([K, V], (nth1(I, X_Y_Z, K), nth1(I, A_B_C, V)), XA_YB_ZC).