リスト組み合わせ(ランダム選択)

test:-
	List1=[1,2,3],
	bagof(X,perm(List1,X),List2),
	lleng(List2,N),
	NN is N -1,
	s_random(NN,X),
	choicelist(X,List2,List3),write(List3).
 
choicelist(0,[A|L],A):- !.
choicelist(N,[A|L],X):-
	NN is N-1,
	choicelist(NN,L,X).
 
perm([],[]).
perm(Xs, [Z | Zs]) :- select(Z, Xs, Ys), perm(Ys, Zs).
 
select([],[],[]):- fail.
select(A,[A|L1],L1).
select(X,[A|L1],[A|L2]):-
		select(X,L1,L2).
 
lleng([],0):- !.
lleng([A|L],NN):-
	lleng(L,N),
	NN is N +1.
 
%||?-test.
%[2,1,3]
%yes
%LOOP = 88
%||?-test.
%[1,3,2]
%yes
%LOOP = 86
%||?-test.
%[1,3,2]
%yes
%LOOP = 86
%||?-test.
%[2,3,1]

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2012年09月05日 18:32