获取列表中元素的所有组合(Prolog)

时间:2018-11-16 03:19:38

标签: prolog

我正在尝试与Prolog中的list一起练习。 实际上,我尝试做的是: 获取列表中元素的所有组合, 确定结果列表是否长于输入列表。这是代码:

%

descartes([],[],false).

descartes(X,Result,IsExist):-
i_think(X,Result),
i_exists(X,Result,IsExist).

%should concatenate all combinations of input list
i_think([],NULL).

i_think([X|Xs],S):-
Y = Xs,
Y1 = X,
concatenate([X,Y1],Z1),
i_think(Y,T),
i_think(Xs,U),
S = [Z1,T,U].

%
concatenate([X,Y],R):-
 atomics_to_string([X,Y],T),
 atomics_to_string([Y,X],U),
R= [T,U].
%
i_exists(X,Y,false):-
len(X,Y,2);
len(X,Y,1).
i_exists(X,Y,true):-
len(X,Y,0).
%
len(X,Y,Count) :-
   length(X,K),

   length(Y,I),
    (
        (
            K < I,
            Count is 0
        );
        (
            I < K,
            Count is 2
        );
        (
            I = K,
            Count is 1
        )
    ).

主要问题在于i_think / 2,感谢您的帮助。 .... 当然,请指定I / O,并感谢您的答复:

descartes/3 : 
descartes(['gold','bank'],Result,isExist)
Result=['goldgold','goldbank','bankbank','bankgold']
isExist= true
(In fact I would prefer to erase repetitions as...)
descartes(['gold','gold'],Result,isExist)
Result=['goldgold','goldgold','goldgold','goldgold'] 
(this shouldn't be generated as i_exist would return true)
isExist= false

0 个答案:

没有答案