Think inductively. I'm asserting that the length of the empty list and a list of one list are "equal" (there are no/just one other to compare to). Then I say if the length of the first item equals the length of the second item, as long as the lengths of the second item and the rest match we are good.

Note: we're not explicitly saying that the length of X and Y is the same with some kind of equality test. We're letting Prolog handle that for us, by simply declaring that the length of X and Y is Len. So if the length of Y doesn't unify with the length of X, the predicate will fail.

Reversing the process

So, to write a predicate to determine if none of the lists have the same length we must observe that this time we will have to keep track of which lengths have been seen so far to check against. We must compare each list's length to all the preceeding list lengths to determine inequality. So this time our initial case will create the initial list of lengths and defer processing to another predicate like so:

So we're calculating the length of this list, then asserting that this is not a length we have seen before with the handy member predicate, then passing this length along with the rest along to the remainder of the list.

Addendum: unequal w/ forall

Inspired by the other answers, you can implement unequal_lengths in a higher-order fashion like so:

If you think about it, this corresponds quite closely to a formal logic expression of the problem: for every list length, there does not exist an element of the remaining list lengths corresponding to this one.

Related Articles

I have two lists of the same length a = [[1,2], [2,3], [3,4]] b = [[9], [10,11], [12,13,19,20]] and want to combine them to c = [[1, 2, 9], [2, 3, 10, 11], [3, 4, 12, 13, 19, 20]] I do this by c= [] for i in range(0,len(a)): c.append(a[i]+ b[i]) Howe

I'm working with two lists of characters and I want to check if they have the same elements except one in the same position, like this: compare([L1,L2,L3,L4],[W1,W2,W3,W4]) :- ((W1 \= L1, W2 = L2, W3 = L3, W4 = L4); (W1 = L1, W2 \= L2, W3 = L3, W4 =

How can I write a relation in prolog that determines if there are any two pairs in a list with the same sum. The relation should fail if there exist no pairs whose sums are equal. The relation should also fail if the list contains less than four elem

This has been happening recently, and I cannot understand how to resolve. N.B. I am using rStudio v0.99.893 I have created a character vector from a data.table, which I then attempt to View, and receive the above error: Error in View : 'names' attrib

I have 2 arrays: $array1 = array(1 => '12', 2 => '4', 3 => '54'); $array2 = array(1 => '12', 2 => '4', 3 => '54', 4 => '124'); How would I go about making $array2 have the same number of keys as $array1, and removing any at the end. S

Would I be right in saying, Skip Lists like arrays can be very efficient if one knows the 'n' (number of elements to be stored) beforehand? Maxlevel of a skip list is (log n + 1), and since I need to know the maxlevel before creating the skip list it

I have just started with Prolog and now I have my first problem, which I just can't solve: I want to give the program 3 lists and as result I want to know if their sum is equal. So something like this: ?- sum_equal([1,2,3],[4,1,1],[5,1,0]). true Unti

I know how to delete an element in a list when it does not have a certain size like: x = [[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2],[1,2,3],[1,2,3],[1,2,3,4]] y = [s for s in x if len(s) == len(x[0])] Where x is the original list, and y is the ne

I am new to python and I was just trying some list manipulations. I have three lists A = [10,20,30,40,50,60] B = [22,44,66,88,12,10] C = [2,4,6,8,10,20] I want to iterate over these three lists and for each value of C I want to add half of that value

This is what I want to achieve: public ArrayList<Point> startPoints = new ArrayList<Point>(); public ArrayList<Point> endPoints = new ArrayList<Point>(); for (Point startPoint : startPoints) { // <-- How do I do I do 2 at the sa

Possible Duplicate: Comparing two collections for equality I need to verify if two IEnumerable<T> lists have the same elements, not necessarily in the same order. I'm targetting .NET 3.5. Here are the tests. The question is, how should HasSameElemen

Given a set of dictionaries of int*string where the first is the "primary", I want to answer the question: For all additional dictionaries, do they all have the same values for the same keys as the primary? I've currently achieved this by doing

This question already has an answer here: check if all elements in a list are identical 19 answers In some of my code I put a series of objects in a list and I build an additional list out of their attributes, which is a string. I need to determine i