开发者

Prolog Problem with combinding predicates that work on their own

开发者 https://www.devze.com 2022-12-10 07:27 出处:网络
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.

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 开发者_JAVA百科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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号