Prolog列表列表获取所有元素 [英] Prolog list of lists get all elements

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

问题描述

我有一个列表:

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.

我的目标是首先返回只有一个值的列表.然后一一返回其他列表的所有元素.我有代码的表格,只返回列表的第一个元素,因为我有头部调用.我如何不仅可以返回头部值,还可以返回所有其他值,并且不重复?我试图创建一个函数来返回列表的所有元素.

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).

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

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