Sunday 15 June 2014

prolog - Removing list within lists based on nth element -


I've found a list of lists, for example:

  [[1, 2, 3, 2], [1, 3, 4, 3], [1, 4, 5, 4], [2, 3, 5, 6], [1, 5, 6, 5], [2, 4, 6, 8], [1, 6, 7, 6], [2, 5, 7, 10], [3, 4, 7, 12], [2, 6, 8, 12]]  

I would like to get the last element and check to see if any other list is similar to the 4th element. If this is the same, leave the list alone, but if it is unique then remove the list. So in the above example, I will be left with:

  [[3, 4, 7, 12], [2, 6, 8, 12]]  

Actually I want to remove all the lists where the last element is unique. I wrote a statement to get the nth element:

  my_membership (x, [x | _ ]). My_membership (X, [_ | Tail]): - my_membership (X, Tale).  

Where:

  my_membership ([_, _, _, fourth], [[3, 4, 7, 12], [2, 6] ], 8, 12]]).  

returns:

  fourth = 12th = 12  

Start by creating two basic predictions:

  last ([X], X). Previous ([_ | T], X): - Last (T, X). Next ([_, _, _, F | _], F) The first bill removes the last element of the list; The second undoubtedly removes the element next to a list.  

Now you can make a decision that how many tamis appear to be an element X list of lists below, h in [ H | T] is a list:

  matching_fthth ([], _, 0). Milan_of orth ([H.Te], X, R): - Forward (H, X), Milan_of orth (T, X, RR), RR + 1. With these conditions, you can make a bill to check your status: Milan_f orth ([_ | T], X, R): - Milan_f orth (T, X, R). There will be three sections in its list - when the list is empty, then when it matches any other list in the top list, and when it does not happen for the circumstances:  
  my_membership ( [], [], _). My_membership ([H. T], [H], A): - Last (H, X), Milan_of orth (A, X, C), C & gt; 1, my_membership (T, R, A). My_membership ([_ | T], R, A): - my_membership (T, R, A)  

The first and last section is self explanatory, the middle section removes the last element from the main list , How many times it matches the element next to the original list of lists (stands for A "Everyone"), and when there was a match for the H result Adding is done through the integration with the head of the result list.

Finally, you need my_membership / 2 to start the recursive series that will go through the basic list of lists:

  My_membership (L, R): - my_membership (L, R, L)  


No comments:

Post a Comment