Prolog创建列表 [英] Prolog creating lists

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

问题描述

我正在尝试用Prolog编写一个程序,该程序将包含三个列表(所有长度均相同)并返回一个列表列表.

I'm trying to write a program in Prolog that will take in three lists (all of which are the same length) and return a list of lists.

我要返回的列表列表是一个三元组,其中包含要传入的三个列表中的元素.三元组的第一个元素来自传入的第一个列表,三元组的第二个元素来自于第二个列表,三元组的第三个元素来自传入的第三个列表.

The list of lists that I am returning is a triple that contains elements from the three lists that are being passed in. The first element of the triple is from the first list passed in, the second element of the triple is from the second list, and the third element of the triple is from the third list passed in.

我想要发生的是函数返回的三元组列表,以返回您可以从传入的三个列表中得出的每个单一可能组合.

What I want to have happen is the list of triples that the function is returning to return every single possible combination that you could make from the three lists being passed in.

截止到现在,我有一些代码将三个列表的第一个元素取出来,使它们变成三倍,然后使所有列表的第二个元素取出并使其成三倍,依此类推.在下面.

As of now I have some code that takes the first elements of the three lists and makes a triple out of them, then takes the second element of all the lists and makes a triple out of them, and so on. Here it is below.

   listCombos( [], [], [], []).
   listCombos( [A|AREST], [B|BREST], [C|CREST], [[A,B,C]|SOLUTION]) :-
       listCombos( AREST, BREST, CREST, SOLUTION).

我获取每个组合的策略是获取第一个列表的第一个元素和第二个列表中的第一个元素,然后遍历第三个列表中的每个元素.完成这些操作后,我将继续移动第一个列表中的第一个元素和第二个列表中的第二个元素,并将它们与第三个列表中的每个元素进行匹配.然后,在我浏览完第二个列表之后,移至第一个列表.让我知道是否需要对此进行更多说明.

My strategy for getting every combo is taking the first element of the first list and the first element in the second list and then going through each elements in the third list. Once I have done that I will move on the the first element in the first list and the second element in the second list and match those up with each element in the third list. Then after I have went through the second list move onto the first list. Let me know if more clarification on this is needed.

我是Prolog的新手,所以我不明白如何将我打算做的事情变成代码.我已经尝试了一些方法,但是没有成功,并且得到了一些我不理解的错误代码,因此很难判断我是否朝着正确的方向前进(如果需要,我可以发表一些尝试).如果有人对我应该前进的方向有所了解,或者对我需要做的事情有一些解释,那将不胜感激.

I'm new to Prolog so I don't understand how to turn what I'm planning to do into code. I've tried a few things but haven't been successful and have gotten some error codes I don't understand so it's hard to tell if I'm going in the right direction (I can post some of my attempts if needed). If anyone has some idea of what direction I should go in or some explanation on what I need to do that would be appreciated.

非常感谢您.

推荐答案

了解一些Prolog,最明显的解决方案是这样的:

Knowing a little Prolog the most obvious solution is something like this:

listCombos(Xs, Ys, Zs, Result) :-
    findall([X,Y,Z], 
            (member(X, Xs), member(Y, Ys), member(Z, Zs)),
            Result).

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

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