列表的序言列表获取所有元素 [英] Prolog list of lists get all elements
问题描述
我有一个列表列表:
decide([[1,2,-3],[-2,3],[6],[4]],K).
我想按;"返回所有可能的解决方案.
I want to return all the possible solutions pressing ';'.
规则是首先返回其列表大小为1的值.
The rule is to first return the values that its list has size 1.
然后我要返回其大小大于1的值.
Then I want to return the values that its size is bigger than 1.
size([],0).
size([_|Xs],L) :- size(Xs,N),L is N+1.
head([],[]).
head([X|_],X).
return_list_members([X|_], X).
return_list_members([_|T], X):-return_list_members(T, X).
decide([], []).
decide([L|Ls], Lit):- size(L, N), N == 1, head(L, Lit).
decide([L|Ls], Lit):- size(Ls, N), N == 0, head(L, Lit), !.
decide([L|Ls], Lit):- decide(Ls, Lit) ,return_list_members(Ls, Lit)
示例结果应该如何:
? - decide([[1,2,-3],[-2,3],[6],[4]],K).
K = 6 ;
K = 4 ;
K = -2 ;
K = 3 ;
K = -3 ;
K = 2 ;
K = 1.
我的目标是首先返回仅包含一个值的列表.然后,一一返回其他列表的所有元素.我有代码的形式,只返回列表的第一个elem,因为我有head调用.我如何不但可以返回头值,还可以返回所有其他值?我试图创建一个函数以返回列表的所有元素.
My goal is to return first the list with only one value. Then return all the elem of the others lists, one by one. The form I have the code, only return the first elem of the list, because I have the head call. How I can return not only the head values, but all the others, and without repetead? I tried to creat a function for return all the elem of the lists.
有什么建议吗?
推荐答案
以您的其他问题为起点,只需插入新要求:
Taking your other question as a starting point, simply insert your new requirements:
listoflist_member(Xss, X) :-
( Xs = [_] ; Xs = [_,_|_] ), % new
member(Xs, Xss),
member(X, Xs).
这篇关于列表的序言列表获取所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!