[英]lists of lists in prolog
I have a lists which contains 2 lists: the first one is a lists of lists and the second one the same. 我有一个包含2个列表的列表:第一个是列表列表,第二个是相同列表。 I want to make a list of lists which contains tuples with three elements. 我要列出包含三个元素的元组的列表。 This is my initial list : 这是我的初始清单:
[ [ [1 2 3 4 a], [aa, bb], R1], [[ 1 3 4 5 b], [X, Y] , R2]] , [anything]]
I want to obtain this list: 我要获取此列表:
[[a, [aa, bb], R1], [b, [x, y], R2]]
How can I implement a predicate to return me the list above in prolog? 如何实现谓词以在序言中将上面的列表返回给我? I have tried this : 我已经试过了:
get_game_tiles([[[_,_,_,_,I],[X,Y],R]|Tail], [[Elem1,Elem2|Tail]]) :-
findall(S,
( member(S,[[[_,_,_,_,S], [X,Y], R], [[_,[_]]]]), S = I ),
[[Elem1,_|_]]),
findall([A,B],
( member([A,B] ,[[[_,_,_,_,S], [A,B], R], [[_,[_]]]]),
A = X, B = Y
) ,
[[_,Elem2|_]]),
findall(Z,
( member(Z ,[[[_,_,_,_,S], [A,B], Z], [[_,[_]]]]),
Z = R
),
[[_,_|Tail]]).
Your solution with findall
calls is very over-done. 使用findall
调用的解决方案非常超额。 There are some issues, such as the following: 存在一些问题,例如:
member(S,[[[_,_,_,_,S], [X,Y], R], [[_,[_]]]])
and 和
member(Z ,[[[_,_,_,_,S], [A,B], Z], [[_,[_]]]])
These are a cyclic terms. 这些是循环术语。 You're querying whether S
is a member of the list, [[[_,_,_,_,S], [X,Y], R], [[_,[_]]]]
. 您正在查询S
是否为列表[[[_,_,_,_,S], [X,Y], R], [[_,[_]]]]
。 S
is embedded in a term within an element of the list you are asking if S
is a member of. S
嵌入在您要询问S
是否为其成员的列表元素中的术语中。 Likewise, you're asking if Z
is a member of the list, [[[_,_,_,_,S], [A,B], Z], [[_,[_]]]]
. 同样,您要询问Z
是否是列表[[[_,_,_,_,S], [A,B], Z], [[_,[_]]]]
。 Z
is embedded in a term within an element of the list you are asking if Z
is a member of. Z
嵌入在您要询问Z
是否Z
成员的列表元素中的术语中。
member([A,B] ,[[[_,_,_,_,S], [A,B], R], [[_,[_]]]])
This isn't so much a problem but will always be false since the list consists of the following elements, neither of which match [A,B]
(a list of two elements): 这并不是什么大问题,但是由于列表由以下元素组成,因此始终都是错误的,这些元素都不匹配[A,B]
(两个元素的列表):
[[_,_,_,_,S], [A,B], R] % a list of three elements
[[_, [_]]] % a list of one element ([_, [_]])
Assuming I understand the conditions of your problem (I am not yet convinced 100% that I do), the approach in Prolog is actually much simpler: 假设我了解您的问题的状况(我尚未100%地相信我的理解),那么Prolog中的方法实际上要简单得多:
get_game_tiles([], []). % Empty list maps to empty list
get_game_tiles([S|_], []) :- % List starting with non-matching pattern results in
S \= [[_,_,_,_,_], [_,_], _]]. % empty and ends recursion
get_game_tiles([[[_,_,_,_,X], [A,B], R]]|T], [[X, [A,B], R]|TR]) :-
get_game_tiles(T, TR).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.