Как печатать только один раз в Прологе во время рекурсивного вызова - PullRequest
0 голосов
/ 24 ноября 2018

У меня есть следующий код, над которым я работал для экспертной системы:

in(switzterland, 'prairie dog').
in(austria,'wild dog').
in(czechia, 'giant panda').
in(america, 'red kangaroo').


linked(switzterland, austria).
linked(austria, czechia).
linked(czechia, america).

tour(X, Y) :-
    linked(X, Y),
    in(X, Z), habitant(Z, I),
    format('~s~t~14|~s~t~31|', ['Animal Name: ',Z]),
    format('~s~t~8|~s~t~23|', ['Habitant: ',I]),
    format('~s~t~8|~s~t~25|', ['Region: ',X]), nl,
    in(Y, U), habitant(U, T),
    format('~s~t~14|~s~t~31|', ['Animal Name: ',U]),
    format('~s~t~8|~s~t~23|', ['Habitant: ',T]),
    format('~s~t~8|~s~t~25|', ['Region: ',Y]), nl,!.
tour(X, Y) :-
    format('~s~t~14|~s~s~s\n~s', ['Dear customer, your tour is from: ',X,
       ' to ', Y,'Through your visit, you\'ll be able to see the following animals, please enjoy.']),nl,nl,
    in(X, Z), habitant(Z, I),
    format('~s~t~14|~s~t~31|', ['Animal Name: ',Z]),
    format('~s~t~8|~s~t~23|', ['Habitant: ',I]),
    format('~s~t~8|~s~t~25|', ['Region: ',X]), nl,
    linked(X, F), tour(F, Y).

вывод:

Dear customer, your tour is from: switzterland to america
Through your visit, you'll be able to see the following animals, please enjoy.

Animal Name:  prairie dog      Habitant: grasslands     Region: switzterland     
Dear customer, your tour is from: austria to america
Through your visit, you'll be able to see the following animals, please enjoy.

Animal Name:  wild dog         Habitant: grassland      Region: austria          
Animal Name:  giant panda      Habitant: open forest    Region: czechia          
Animal Name:  red kangaroo     Habitant: woodlands      Region: america  

Вы видите, что «Уважаемый клиент .... 'повторяется дважды, или каждый раз, когда появляется рекурсивный вызов второго тура, он печатается снова.Я только хочу, чтобы это было напечатано один раз.

1 Ответ

0 голосов
/ 24 ноября 2018

Вам нужны два предиката, первый (например, tour/2), печатающий сообщение «Уважаемый клиент ...» и вызывающий второй предикат (например, find_tour/2), который вычисляет маршрут (ы).Например:

tour(X, Y) :-
    format('~s~t~14|~s~s~s\n~s', ['Dear customer, your tour is from: ',X,
       ' to ', Y,'Through your visit, you\'ll be able to see the following animals, please enjoy.']),nl,nl,
    find_tour(X, Y).

find_tour(X, Y) :-
    linked(X, Y),
    in(X, Z), habitant(Z, I),
    format('~s~t~14|~s~t~31|', ['Animal Name: ',Z]),
    format('~s~t~8|~s~t~23|', ['Habitant: ',I]),
    format('~s~t~8|~s~t~25|', ['Region: ',X]), nl,
    in(Y, U), habitant(U, T),
    format('~s~t~14|~s~t~31|', ['Animal Name: ',U]),
    format('~s~t~8|~s~t~23|', ['Habitant: ',T]),
    format('~s~t~8|~s~t~25|', ['Region: ',Y]), nl,!.
find_tour(X, Y) :-
    in(X, Z), habitant(Z, I),
    format('~s~t~14|~s~t~31|', ['Animal Name: ',Z]),
    format('~s~t~8|~s~t~23|', ['Habitant: ',I]),
    format('~s~t~8|~s~t~25|', ['Region: ',X]), nl,
    linked(X, F),
    find_tour(F, Y).
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...