0

Given multiple lists with sub-lists, how do you check for equivalence? For example, you are give data sets like this:

table(book,
       [[["A"], ["A"], ["B", "C"], ["B", "C"]],
        [["C"], ["S", "A"], ["S", "B", "A"]],
        [["C", "A"], ["C", "S", "A"]],
        [["C", "B", "S", "A"]]]).
table(book3,
       [[["A"], ["A"], ["B", "C"], ["B", "C"]],
        [["C"], ["S", "A"], ["S", "B", "A"]],
        [["C", "A"], ["C", "S", "A"]],
        [["A", "S", "B", "C"]]]).

Now, you have to check if these two tables are equivalent. Here is a sample output for when the two tables are equivalent:

?- table(book,T1),table(book3,T2),table_equivalent(T1,T2).
T1 = [[["A"], ["A"], ["B", "C"], ["B", "C"]],
      [["C"], ["S", "A"],["S", "B", "A"]],
      [["C", "A"], ["C", "S", "A"]], [["C", "B", "S", "A"]]],
T2 = [[["A"], ["A"], ["B", "C"], ["B", "C"]],
      [["C"], ["S", "A"],["S", "B", "A"]],
      [["C", "A"], ["C", "S", "A"]],
      [["A", "S", "B", "C"]]].

I have a predicate that checks the equivalence of two lists with sub-lists:

row_equivalent(RowA,RowB) :-
   check_sublist_equivalence(RowA, RowB).

check_sublist_equivalence([],[]).
check_sublist_equivalence([H|T],[H1|T1]) :-
   rows(H,H1),
   check_sublist_equivalence(T,T1).

rows([],_).
rows([H2|T2],K) :- member(H2,K), rows(T2,K).

Is there any way I can use this predicate to check the equivalence of the above two tables?

false
  • 10,264
  • 13
  • 101
  • 209
  • I've answered a souspiciously similar question 20 hours ago here: https://stackoverflow.com/questions/49063131/equating-a-sublist-to-another-sublist-for-cyk-table-in-prolog/49070506#49070506 – damianodamiano Mar 03 '18 at 10:48
  • Did you write that `row_equivalent` predicate? – lurker Mar 03 '18 at 13:23
  • @damianodamiano Yes, I've been trying to use that predicate (thanks, by the way because it helps me understand the H|T concept of prolog much better) to compare two tables instead of two lists. So, I'm trying to do this table(Table1,Table2):- check_tables(Table1,Table2). check_tables([[]],[[]]). check_tables([[H|T]],[[H1|T1]]) :- row_equivalent([H],[H1]),check_tables(T,T1). But this isn't working. – ilovecoding Mar 03 '18 at 14:27
  • *But this isn't working* is a very vague problem statement. – lurker Mar 03 '18 at 15:36
  • The code compiles, but it is giving me a "false" for the output. So, it is not giving me the output like in the sample output of the question. – ilovecoding Mar 03 '18 at 20:27

0 Answers0