由于您的关系是描述列表,您可以选择使用 DCG。您可以这样描述互补的核碱基:
complementary(t) --> % thymine is complementary to
[a]. % adenine
complementary(a) --> % adenine is complementary to
[t]. % thymine
complementary(g) --> % guanine is complementary to
[c]. % cytosine
complementary(c) --> % cytosine is complementary to
[g]. % guanine
这对应于您的谓词对/2。要以相反的顺序描述键合序列,您可以这样进行:
bond([]) --> % the empty sequence
[]. % doesn't bond
bond([A|As]) --> % the sequence [A|As] bonds with
bond(As), % a bonding sequence to As (in reverse order)
complementary(A). % followed by the complementary nucleobase of A
通过首先编写递归目标,然后将描述互补核碱基的目标描述到列表头部的目标,可以实现相反的顺序。您可以像这样使用短语/2 进行查询:
?- phrase(bond([t,t,a,c]),S).
S = [g,t,a,a]
或者您可以使用包含短语/2 的单个目标的包装谓词:
seq_complseq(D,M) :-
phrase(bond(D),M).
然后查询它:
?- seq_complseq([t,t,a,c],C).
C = [g,t,a,a]
我发现带有 DCG 的列表的描述比相应的谓词版本更容易阅读。当然,以相反的顺序描述互补序列是一项相对容易的任务。但是一旦你想描述更复杂的结构,比如cloverleaf structure 的tRNA DCGs 就派上用场了。