0
votes

For example there are two lists

List1 = [ [1,2,3,4], [2,4,5,4],[4,5,2,5],.....]
List2= [ [2,4,4,1], [5,6,1,2], [4,2,3,4],.....]

now i want to look the two list, and compare the third element of each list inside of list 1, that of with list2. If they are same then leave the the list inside of the list1, and continue with next one. if they are not the same put the lists in side of list1 in different list(i.e list3). So something like

compare([][][]).
compare([[firstele, second ele, third ele, fourth ele]|list1],[firstele2, second ele2, third ele2, fourth ele2]|list2],list3):-
   ....... condition......

how would i do this?

1
Can you add expected output? - C.B.

1 Answers

0
votes

Take a look at the following code:

convert([],[]).
convert([L|Lt],[Q|Qt]) :- L>64, L<91, Q is L+32, convert(Lt,Qt).
convert([L|Lt],[L|Qt]) :- convert(Lt,Qt).

Prolog always look for the 1st true. So, if line1 is not true then it will go to line2 and if line2 is not true it will go to line3. Each line to be true all the statements have to be true. With this logic if both L>64, L<91 becomes true then Q is L+32, convert(Lt,Qt) will be executed (both of them will be true line 3 will not be executed). However, if either of L>64, L<91 is false line 3 will be executed. So the traditional program (if you would write the above prolog program in C or Java) will be

if(L>64 && L<91){
  Q=L+32;
  convert(Lt,Qt);
}
else if (the 1st of each list is same){
  convert(Lt,Qt);
}

Hope this helps. (I did not understand your exact question, but it seemed like you are looking for how to write conditions.)