Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Prolog中的打印格式问题_Prolog_Format - Fatal编程技术网

Prolog中的打印格式问题

Prolog中的打印格式问题,prolog,format,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

我有以下代码:

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只的动物(其余的都在这个输出中重复,我不知道为什么)。然后是其他产卵的动物。

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

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
谓词的所有解决方案。第二条也类似。最后一个子句只是在打印所有动物的信息后成功调用
所有/0
谓词。

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

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
谓词的调用成功

我希望第三列对齐

我通常不使用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.

首先要打印所有只生了4个孩子的动物(其余的都在这个输出中重复,我不知道为什么)。然后是其他产卵的动物

在我写这篇文章的时候,Paulo Moura刚刚发布了这部分内容,这部分内容和我计划给出的答案是一样的,就是用a和一个三分句谓词

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.
我希望第三列对齐

我通常不使用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.

首先要打印所有只生了4个孩子的动物(其余的都在这个输出中重复,我不知道为什么)。然后是其他产卵的动物

在我写这篇文章的时候,Paulo Moura刚刚发布了这部分内容,这部分内容和我计划给出的答案是一样的,就是用a和一个三分句谓词

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.
感兴趣的:和感兴趣的:和