Prolog中的打印格式存在问题

时间:2018-11-22 13:30:31

标签: prolog format

我有以下代码:

everything:-give_birth(X), give_eggs(Y),
    format('Animal Name: \t~w,  \tGives Birth', X), nl,  
    format('Animal Name: \t~w,  \tGives egg', Y), nl,fail.

这是输出:

Animal Name:    cheetah,    Gives Birth
Animal Name:    ostrich,    Gives egg
Animal Name:    cheetah,    Gives Birth
Animal Name:    penguin,    Gives egg
Animal Name:    cheetah,    Gives Birth
Animal Name:    albatross,  Gives egg
Animal Name:    tiger,  Gives Birth
Animal Name:    ostrich,    Gives egg
Animal Name:    tiger,  Gives Birth
Animal Name:    penguin,    Gives egg
Animal Name:    tiger,  Gives Birth
Animal Name:    albatross,  Gives egg
Animal Name:    giraffe,    Gives Birth
Animal Name:    ostrich,    Gives egg
Animal Name:    giraffe,    Gives Birth
Animal Name:    penguin,    Gives egg
Animal Name:    giraffe,    Gives Birth
Animal Name:    albatross,  Gives egg
Animal Name:    zebra,  Gives Birth
Animal Name:    ostrich,    Gives egg
Animal Name:    zebra,  Gives Birth
Animal Name:    penguin,    Gives egg
Animal Name:    zebra,  Gives Birth
Animal Name:    albatross,  Gives egg

第一个问题是:     我希望第三列对齐。

第二个问题:输出不是我想要的,我要它首先打印所有只有4个出生的动物(其余的在此输出中重复,我不知道为什么)。然后剩下的其他产卵的动物。

2 个答案:

答案 0 :(得分:2)

关于第二个问题,请尝试:

everything :-
    give_birth(X),
    format('Animal Name: \t~w,  \tGives Birth', X), nl,
    fail.
everything :-
    give_eggs(Y),
    format('Animal Name: \t~w,  \tGives egg', Y), nl,
    fail.
everything.

此代码将首先打印所有会出生的动物,然后打印所有会产生卵的动物。它使用通常称为故障驱动的循环。第一个子句中对fail/0的调用导致回溯到give_birth /1谓词的所有解决方案。第二条类似。在打印了所有动物的信息后,最后一个子句只是使对everything/0谓词的调用成功。

答案 1 :(得分:2)

  

我希望第三列对齐。

我通常不将format/2与Prolog一起使用,因为选项卡的概念会让我发疯。另外,我主要使用Prolog解决AI问题,而不是使用UI,因此我习惯于阅读和构造嵌套结构。

这不是如何使用format/2来对齐代码的最佳答案,但是它可以工作。

everything_3 :-
    give_birth(X),
    give_eggs(Y),
    format('~s~t~14|~s~t~25|~s~t~25|~n', ['Animal Name: ',X,'Gives Birth']),
    format('~s~t~14|~s~t~25|~s~t~25|~n', ['Animal Name: ',Y,'Gives egg']),
    fail.

使用

give_birth(cheetah).
give_birth(tiger).
give_birth(zebra).
give_eggs(ostrich).

?- everything_3.
Animal Name:  cheetah    Gives Birth
Animal Name:  ostrich    Gives egg
Animal Name:  tiger      Gives Birth
Animal Name:  ostrich    Gives egg
Animal Name:  zebra      Gives Birth
Animal Name:  ostrich    Gives egg
false.
  

首先打印所有只有4个已出生的动物(其余输出在此输出中重复,我不知道为什么)。然后剩下的其他产卵的动物。

在撰写本文时,Paulo Moura刚刚发布了answer的这一部分,这与我计划给出的答案相同,就是使用带有三个子句谓词的failure-driven loop

everything :-
    give_birth(X),
    format('Animal Name: \t~w,  \tGives Birth', X), nl,
    fail.
everything :-
    give_eggs(Y),
    format('Animal Name: \t~w,  \tGives egg', Y), nl,
    fail.
everything.

这是结合了示例运行的两个答案。

everything_4 :-
    give_birth(X),
    format('~s~t~14|~s~t~25|~s~t~25|~n', ['Animal Name: ',X,'Gives Birth']),
    fail.

everything_4 :-
    give_eggs(Y),
    format('~s~t~14|~s~t~25|~s~t~25|~n', ['Animal Name: ',Y,'Gives egg']),
    fail.

everything_4.

?- everything_4.
Animal Name:  cheetah    Gives Birth
Animal Name:  tiger      Gives Birth
Animal Name:  zebra      Gives Birth
Animal Name:  ostrich    Gives egg
true.