list prolog bioinformatics swap dcg

其他 - 如何在序言列表中交换三乘三元素？

``````| ?- swap([c,g,g,a,t,t,g,c,a,a], X).

X = [a,t,t,c,g,g,g,c,a,a]
X = [g,c,a,a,t,t,c,g,g,a]
X = [c,g,g,g,c,a,a,t,t,a]
X = [c,a,a,a,t,t,g,c,g,g]
.
.
.
``````

``````swap([H1, H2, H3, H4, H5, H6|T1], X) :-
X = [H4, H5, H6, H1, H2, H3|T1];
swap([H2, H3, H4, H5, H6|T1], X);
swap([H1, H2, H3, H4, H5|T1], X).
``````

``````| ?- swap([c,g,g,a,t,t,g,c,a,a], X).

X = [a, t, t, c, g, g, g, c, a, a] ;
X = [t, t, g, g, g, a, c, a, a] ;
X = [t, g, c, g, a, t, a, a] ;
X = [g, c, a, a, t, t, a] ;
X = [c, a, a, t, t, g] ;
X = [c, a, a, a, t, t] ;
X = [g, c, a, g, a, t, a] ;
X = [c, a, a, a, t, g] ;
X = [c, a, a, g, a, t] ;
X = [t, g, c, g, g, a, a, a] ;
X = [g, c, a, g, a, t, a] ;
X = [c, a, a, a, t, g] ;
X = [c, a, a, g, a, t] ;
X = [g, c, a, g, g, a, a] ;
X = [c, a, a, g, a, g] ;
X = [c, a, a, g, g, a] ;
X = [t, t, g, c, g, g, c, a, a] ;
X = [t, g, c, g, g, t, a, a] ;
X = [g, c, a, g, t, t, a] ;
X = [c, a, a, t, t, g] ;
X = [c, a, a, g, t, t] ;
X = [g, c, a, g, g, t, a] ;
X = [c, a, a, g, t, g] ;
X = [c, a, a, g, g, t] ;
X = [t, g, c, c, g, g, a, a] ;
X = [g, c, a, g, g, t, a] ;
X = [c, a, a, g, t, g] ;
X = [c, a, a, g, g, t] ;
X = [g, c, a, c, g, g, a] ;
X = [c, a, a, g, g, g] ;
X = [c, a, a, c, g, g] ;
false.
``````

RobertM

87
false 2020-02-03 03:21

``````:- set_prolog_flag(double_quotes, chars).
``````

``````... Triple1 ... Triple2 ...  is the OldSequence

... Triple2 ... Triple1 ...  is the NewSequence
``````

``````tripleswap(OldSequence, NewSequence) :-
dif(T1,T2),
phrase( ( seq(A), triple(T1), seq(B), triple(T2), seq(C) ), OldSequence),
phrase( ( seq(A), triple(T2), seq(B), triple(T1), seq(C) ), NewSequence).

seq([]) --> [].
seq([B|Bs]) --> [B], seq(Bs).

triple([A,B,C]) --> [A,B,C].
``````

``````?- phrase(triple(T1), Bs).
T1 = Bs, Bs = [_A,_B,_C].
``````

`seq//1` 是一个任意长的序列。

``````samelength([], []).
samelength([_|Xs], [_|Ys]) :-
samelength(Xs, Ys).
``````

``````?- tripleswap("cggattgcaa", Bs).
Bs = "attcgggcaa"
;  Bs = "ttgacggcaa"
;  Bs = "tgcatcggaa"
;  Bs = "gcaattcgga"
;  Bs = "caaattgcgg"
;  Bs = "cttgggacaa"
;  Bs = "ctgctggaaa"
;  Bs = "cgcattggaa"
;  Bs = "ccaattggga"
;  Bs = "cgtgcgataa"
;  Bs = "cggcatgata"
;  Bs = "cgcaatggat"
;  Bs = "cgggcaatta"
;  Bs = "cggcaagatt"
;  Bs = "cggacaattg"
;  false.
``````

David B. Searls，《使用定句从句研究DNA的语言学》，NACLP，1989年