Prolog以特定格式打印

时间:2018-02-26 06:41:10

标签: prolog

我现在手上有两个列表[A, B, C][D, E, F]。 list1和list2中的值在相同的范围内,但我不确切知道它们是如何相互匹配的。

有一种可能性A = D = 1, B = E = 2, C = F = 3

根据上述条件,我如何以下列格式打印结果???

(ONE, A, D)

(TWO, B, E)

(THREE, C, F)

最初的问题是,您必须在给定约束下计算list1和list2中每个变量的值。约束条件如下:

A < B, B < C, D < E, E < F, 
the range of [A, B, C] is from 1 to 3, 
the range of [D, E, F] is from 1 to 3, 
[A, B, C] are all different numbers, 
[D, E, F] are all different numbers.

到目前为止,我写的内容如下:

calculate([A, B, C, D, E, F]) :-
    between(1, 3, A),
    between(1, 3, B),
    between(1, 3, C),
    between(1, 3, D),
    between(1, 3, E),
    between(1, 3, F),
    A < B, B < C, D < E, E < F.

在我查阅此代码文件并调用calculate([A, B, C, D, E, F])之后,我得到了最终结果A = D = 1, B = E = 2, C = F = 3,但是如何以优雅的格式打印?

1 个答案:

答案 0 :(得分:-1)

这是最好的!



    ?- writeln('(ONE,A,D)'),writeln('(TWO,B,E)'),writeln('(THREE,C,F)').
    (ONE,A,D)
    (TWO,B,E)
    (THREE,C,F)
    true.