我尝试根据事实创建一个列表:
mother(jane,jerry).
mother(susan,riche).
mother(helen,kuyt).
我希望将母亲的名字转换为包含许多元素的列表,如:
momlist([jane,susan],2).
momlist([jane,susan,helen],3).
momlist([jane],1).
我尝试用以下方法创建:
momlist(X,Number):- mom(X,_),
NewNum is Number-1,
NewNum > 0,
write(x),
momlist(X,NewNum).
它只是写了妈妈姓名的次数......
如何根据这些事实制作清单?
致以最诚挚的问候和感谢。
答案 0 :(得分:2)
这是
mother(jane,jerry).
mother(susan,riche).
mother(helen,kuyt).
mother(govno,mocha).
mother(ponos,albinos).
momlist( X, L ) :-
length( X, L ),
gen_mum( X ),
is_set( X ).
gen_mum( [] ).
gen_mum( [X|Xs] ) :-
mother( X, _ ),
gen_mum( Xs ).
所以
?- momlist(X, 3).
X = [jane, susan, helen] ;
X = [jane, susan, govno] ;
X = [jane, susan, ponos] ;
X = [jane, helen, susan] ;
X = [jane, helen, govno] ;
X = [jane, helen, ponos] ;
X = [jane, govno, susan] ;
并且
?- momlist(X, 2).
X = [jane, susan] ;
X = [jane, helen] ;
X = [jane, govno] ;
X = [jane, ponos] ;
X = [susan, jane] ;
X = [susan, helen] ;
X = [susan, govno] ;
X = [susan, ponos] ;
X = [helen, jane] ;
这就是你想要的吗?
答案 1 :(得分:0)
接受答案的几个小问题:
另一种可能的解决方案是:
momlist(X,L):-
setof(M, C^mother(M,C), AllMoms),
perm(L, AllMoms, X).
perm(0, _, []):- !.
perm(N, From, [H|T]):-
select(H, From, NewFrom),
NewN is N-1,
perm(NewN, NewFrom, T).
如果你不想[jane,helen]以及[helen,jane]等,那么你可以使用子集而不是烫发:
subset(0,_,[]):- !.
subset(N, [M|TM], [M|T]):-
NewN is N-1,
subset(NewN, TM, T).
subset(N, [_|TM], L):-
subset(N, TM, L).