2016-01-07 79 views
3

我的数据库的格式如下:Prolog-追加列表的列表

aminotodna (Amincoacid, [DNA sequence]).

下面是从数据库中的几个例子:

aminotodna(a,[g,c,a]). 
aminotodna(a,[g,c,c]). 
aminotodna(a,[g,c,g]). 
aminotodna(a,[g,c,t]). 
aminotodna(c,[t,g,c]). 
aminotodna(c,[t,g,t]). 
aminotodna(d,[g,a,c]). 
aminotodna(d,[g,a,t]). 
aminotodna(e,[g,a,a]). 
aminotodna(e,[g,a,g]). 
aminotodna(f,[t,t,c]). 
aminotodna(f,[t,t,t]). 

有些氨基酸有多个DNA序列。 这里是我的问题,所以在给定的氨基酸列表中,例如[d,c,e,f],我如何将它们的DNA序列附加在一起并给出所有组合,因为有些序列具有多于一个的序列。

如果是只有两个我能做到这一点,它会只是

listamino(X,Y) :- 
    aminotodna(X,L), 
    aminotodna(Y,M), 
    append(L,M,Z), 
    print(Z). 

;给出了所有的组合。

我已经厌倦了一个清单做,但是这是我的尝试,并没有工作:

listamino([]). 
listamino([H|T]) :- 
    aminotodna(H,L), 
    aminotodna(T,M), 
    append(L,M,X), 
    print(X). 

listamino(T). 
+0

您能根据您的样本数据给出您想要的行为示例吗? –

+0

@ScottHunter所以,如果我写了listamino([a,c,e])。它会附加DNA序列,所以结果将是[g,c,a,t,g,c,g,a,a]和其他所有组合。 – FProlog

+1

完全删除问题的内容真的很不礼貌你已经得到了答案。这使得答案无效并且使已经回答的人的努力失效。 –

回答

1

你需要一个额外的参数,以跟踪当前的组合:

; invoke a version of listamino which tracks the current combination of DNA sequences, which is initially empty 
listamino(X) :- 
    listamino(X,[]). 

; If there are no ore aminos, print the DNA seq list, and we're done 
listamino([],X) :- 
    print(X). 

; Otherwise, append the DNA for the first amino to our list, and process the rest of the mains 
listamino([H|T],X) :- 
    aminotodna(H,L), 
    append(X,L,X2), 
    listamino(T,X2). 
+0

谢谢,这工作。但是如果你可以澄清哪个参数跟踪当前的组合? – FProlog

+0

我加了一个;打印的那个;第二个。 –

+0

我真的很难理解这一点。什么是'listamino(X): - listamino(X,[])。 “在做什么?为什么listamino([H | T],X)中的列表后面有一个X? – FProlog

2

使用Prolog描述列表时,为了方便和清晰,请始终考虑使用DCG符号。例如,使用您的实例的一个子集,我先用DCG规则来描述通信(请注意,我使用的名称是有道理的在各个方向上):

amino_dna(a) --> [g,c,a]. 
amino_dna(a) --> [g,c,c]. 
amino_dna(c) --> [t,g,c]. 
amino_dna(c) --> [t,g,t]. 

的话,我再次使用DCG规则描述这些列表的串联:

aminos([]) --> []. 
aminos([A|As]) --> amino_dna(A), aminos(As). 

样品查询:

?- phrase(aminos([a,c]), As). 
As = [g, c, a, t, g, c] ; 
As = [g, c, a, t, g, t] ; 
As = [g, c, c, t, g, c] ; 
etc. 

没有append/3,没有额外的变量,没有额外的参数,no废话。使用