У меня есть следующий код, над которым я работал для экспертной системы:
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
Вы видите, что «Уважаемый клиент .... 'повторяется дважды, или каждый раз, когда появляется рекурсивный вызов второго тура, он печатается снова.Я только хочу, чтобы это было напечатано один раз.