PROLOG 比较 2 个列表 [英] PROLOG Comparing 2 lists

查看:47
本文介绍了PROLOG 比较 2 个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了很长时间并搜索了很多(...)但什么都没有.

I've been trying for a long time and searched a lot (...) but nothing.

我想要的是创建一个函数,将一个列表的元素与另一个列表的元素进行比较.

What I want is to create a function that will compare elements of one list to the elements of other list.

问题是我完全不知道我将如何遍历两个列表的成员.

The things is I have absolutely no idea how I'm going to iterate through the members of both lists.

例如.将第一个列表 L1 的第一个成员与 L2 的成员进行比较.如果满足某个条件(条件可能会有所不同,但compare/4、member等不行),我会选择L1的第二个成员到L2的所有成员,依此类推.

Ex. comparing the first member of the first list L1 to the members of L2. If it meets a certain condition (the condition may vary, but compare/4, member and such won't do), I'll pick the second member of L1 to all the members of L2, and so on.

问题是在使用 [P|R] 与 L1 的第一个元素进行比较时,我不知道如何在检查其成员后取回完整列表"L2.之后我将如何将 L1 的第二个元素与 L2 的元素进行比较?有什么办法可以将它重置"回第一个位置?

The thing is I don't know how to get back the "full list" L2 after checking through its members while comparing to the first element of L1 by using [P|R]. After that how will I compare the second element of L1 to the elements of L2? Any way to "reset" it back to the first position?

我有类似的东西

function([P|R], [F|L]) :-
   P == F,
   function([P|R],L)
  ;
   P \== F, (guess not necessary)
   function(R, [F|L]).

(它有更多的东西,但这几乎是我想要的基本内容)

(it has more stuff but this is pretty much the basic of what I want)

但它在遍历 1 个列表的所有成员后表现出奇怪的行为.

But it exhibits odd behaviour after going through all the members of 1 list.

是的,这可能是一个非常基本的问题,但 Prolog 只是我不习惯的东西..

And yes, it's probably a very basic question but Prolog is just not something I've gotten used to..

推荐答案

如果您有一个列表的元素以某种方式与另一个列表的所有元素进行比较,然后有一个结果列表,那么您需要在至少 3 个参数.

If you have a list whose elements you are comparing in some way to all of the elements of another list, and then having a resultant list, then you need at least 3 arguments.

compare_list([H1|T1], L, R) :-
    (   check_element(H1, L),     % Compare H1 with elements of L somehow
    ->  R = [H1|T2]               % if the criteria succeeds
    ;   R = T2                    % if the criteria fails
    ),
    compare_list(T1, L, T2).
compare_list([], _, []).

您提到您正在以某种方式将第一个列表的元素与第二个列表的元素进行比较,但这可能会有所不同.所以我只是将其称为谓词 check_element/2,它接受第一个列表中的元素和第二个列表中的元素,并以某种方式比较它们.

You mentioned that you are comparing the elements of the first list with those of the second somehow, but that this may vary. So I just called this a predicate, check_element/2 which accepts the element from the first list and an element from the second list and compares them in some way.

check_element(X, [H|T]) :-
    ... % compare X and H somehow; decide how/when to succeed
    check_element(X, T).
check_element(_, []).

check_element/2 的行为可以随您的需要而变化.它可能会在达到某些条件后完成遍历列表 [H|T] 之前成功,或者可能需要一个元素以某种方式与 [H|T]<的每个元素进行比较/code> 在成功之前.你的选择.如果是后者,可以用maplist的形式替换check_element/2.

The behavior of check_element/2 can vary as you wish. It might succeed before it completes going through the list [H|T] after hitting some criteria, or it may require that an element compare favorably somehow with every element of [H|T] before succeeding. Your choice. If it's the latter, you can replace check_element/2 with a form of maplist.

这篇关于PROLOG 比较 2 个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆