2016-09-23 87 views
1

我尝试定义replaceEltByclass(E1:list,E2:list)E是没有子列表的列表。属于C类的E1的每个元素被C的 元素替代。最终结果被放入E2中。例如,目标rep_class([e1,e4,e6,e11],E2)将提供列表E2:[[e1,e8,e10],e4,[e3,e6,e7],e11]。我没有很好的结果。用特定列表替换列表中的每个元素

/*The code*/ 
/*facts*/ 
class(c1,[e3, e6, e7]). 
class(c2,[e1, e8, e10]). 

/*rules*/ 

rep_class([],[]). 

rep_class([E|Q],E2) :- 
    class(C,L), 
    not(member(E,L)), 
    concat(E2,E,E2), 
    rep_class(Q,E2). 

rep_class([E|Q],E2) :- 
    class(C,L), 
    member(E,L), 
    concat(E2,L,E2), 
    rep_class(Q,E2). 


/*conventional concat*/ 
concat([],L,L). 
concat([H|T],L,[H|Res]) :- concat(T,L,Res). 

回答

0

的问题是在:class(C,L),not(member(E,L)),因为它会得到两种溶液中的一种,如果C = C2,然后实施例E1在C2所属所以它会与L和一个溶液更换它时,C = C1在那里将离开它作为e1。你需要写“C存在这样的成员(e1,L)??”所以你要收集所有可能的列表,看看成员(E1,L)代表名单L.因此,与一些改变我的版本是:

class(c1,[e3, e6, e7]). 
class(c2,[e1, e8, e10]). 

rep_class([],[]). 

rep_class([E|Q],[E|E2]) :- 
    findall(L,class(_,L),List), 
    not(find(List,E,_)), 
    rep_class(Q,E2). 

rep_class([E|Q],[Lout|E2]) :- 
    findall(L,class(_,L),List), 
    find(List,E,Lout), 
    rep_class(Q,E2). 

find([Lin|_],E,Lin):-member(E,Lin). 
find([Lin|T],E,Lout):-not(member(E,Lin)),find(T,E,Lout). 

举个例子:

?- rep_class([e1,e4,e6,e11], E2). 
E2 = [[e1, e8, e10], e4, [e3, e6, e7], e11] ; 
false. 
+0

它正确的行为。我也理解错误和使用findall谓词的必要性。 – Hana

相关问题