John Sall
John Sall

Reputation: 1139

Issue with the format of printing in Prolog

I have the following code:

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.

and this is the output:

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

first problem is: I want the third column to be aligned.

Second problem: the output is not what I want it, I want it first to print all the animals that gives birth which are only 4(the rest are repeated in this output I don't know why). and then the rest of the animals that gives egg.

Upvotes: 1

Views: 65

Answers (2)

Guy Coder
Guy Coder

Reputation: 24976

I want the third column to be aligned.

I don't normally use format/2 with Prolog because the concept of the tabs just drives me nuts. Also I use Prolog mainly for solving AI problems and not doing UI so I am use to reading and constructing nested structures.

This is not the best answer on how to use format/2 to align code but it works.

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.

with

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.

First to print all the animals that gives birth which are only 4(the rest are repeated in this output I don't know why). and then the rest of the animals that gives egg.

As I write this Paulo Moura just posted this part of the answer which is the same answer I was planing on giving, being to use a failure-driven loop with a three clause predicate.

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.

Here are the two answers combined with a sample run.

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.

Upvotes: 2

Paulo Moura
Paulo Moura

Reputation: 18663

Regarding the second problem, try:

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.

This code will print first all animals that give birth, followed by all animals that give eggs. It uses the usually called failure-driven loop. The call to fail/0 in the first clause results in backtracking to all solutions for the give_birth /1 predicate. Similar for the second clause. The last clause simply makes the call to the everything/0 predicate succeed after printing the information for all animals.

Upvotes: 2

Related Questions