views:

136

answers:

2

Here we go, bear with me. The over-all goal is to return the max alignment between two lists. If there are more than one alignment with the same length it can just return the first.

With alignment I mean the elements two lists share, in correct order but not necessarily in order. 1,2,3 and 1,2,9,3; here 1,2,3 would be the longest alignment. Any who, know for the predicates that I already have defined.

align(Xs, Ys, [El | T]) :-append(_, [El | T1], Xs),append(_, [El | T2], Ys),align(T1, T2, T).
align(_Xs, _Ys, []).

Then I use the built-in predicate findall to get a a list of all the alignments between these lists? In this case it puts the biggest alignment first, but I'm not sure why.

findall(X,align([1,2,3],[1,2,9,3],X),L).

That would return the following;

L = [[1, 2, 3], [1, 2], [1, 3], [1], [2, 3], [2], [3], []].

That is correct, but now I need a predicate that combines these two and returns the biggest list in the list of lists.

A: 

So you just need to find the largest item in the list?

Edit:

Ok, so a better answer is this:

If you care about performance, then you need to write your own predicate which scans the list keeping track of the largest item.

If you don't carte so much about performance and you just want it to work, you could just reverse sort it and then take the first item in the sorted list. The advantage of this is that by using a sort library predicate you should be able to implement it in a few lines.

humble coffee
If you by that mean the largest list in the list of lists. Than yes.
Algific
Sorting is an overkill if you just want to find the largest item.
Kaarel
True, but since you could use a library predicate it does have the advantage of being simpler to implement.
humble coffee
+1  A: 

Use the solution given in this answer.

You could also try to avoid using findall/3, because you don't really need to build a list in order to find its largest element.

Kaarel