How to make a variable appear in the write function in Prolog

Asked

Viewed 255 times

0

I’m doing a show that features several dogs, and I want to do it in a way that’s faster to write the characteristics of dogs. For example, the following code

raca(pitbull).
raca(shiba).
raca(boxer).
raca(poodle).
raca(bullte_terrier).
sexo(macho).
sexo(femea).

cor(castanho).
cor(preto).
cor(branco).
cor(tricolor).
cor(pintado).
cor(malhado).

pelo(longo).
pelo(curto).

peso(15kg).




print(X):-
    write(X).

cao(trovao):-
    sexo(macho),
    raca(pitbull),
    cor(marrom),
    pelo(curto),
    write('raca: '),nl,
    write('nascimento: 1999'),nl,
    write('cor: marrom'),nl
    write('pelo: curto').

What I want is a way in write I can print the variable that is saving ifnromation. Example

X = cor(marrom),

write(cor: X).

and then it would be printed in the terminal "color: brown", but it’s not working, I don’t know how to put the variable in write. Can help me?

  • Prolog is a functional language, different from the imperative language like most languages (C, Java, Javascript, ...). In this type of language there are no explicit variables.

  • What you want to return from the function color (brown) ? This function has no return, just like any function of Prolog. It only retouch true or false

  • = does not mean attribution, but equality (in some compilers)

2 answers

0

You can use the format function, which has a printf-like syntax of C, except where C/Java uses '%', Prolog uses '~':

% Note que podemos usar outros caracteres além de ASCII ;)
cão(nome(trovão), raça(pitbull), nascimento(1999), cor(marrom), pelo(curto)).

print_cão(Nome) :-
  % Busca o cão na base de conhecimento.
  cão(nome(Nome), raça(Raça), nascimento(Ano), cor(Cor), pelo(Pelo)),
  % Valida que os dados correspondem às constantes definidas.
  raça(Raça),
  nascimento(Ano),
  cor(Cor),
  pelo(Pelo),
  % Imprime as informações do cão
  format("nome: ~w~nraça: ~w~nnascimento: ~w~ncor: ~w~npelo: ~w~n", [Nome, Raça, Ano, Cor, Pelo]).

0

Hello,

Perhaps a simpler program will help you understand what to do. The program below writes the name of a person’s father:

homem(joao).
homem(jose).

mulher(maria).

pai(joao, jose).
pai(joao, maria).

imprime_pai(X):- homem(Y), pai(Y,X), write('o pai de '), write(X), write(' é '), write(Y),!.

Just save to a file and load (with Consult) into Prolog. Then you can test with:

?- imprime_pai(maria).
o pai de maria é joao
true.

Or:

?- imprime_pai(jose).
o pai de jose é joao
true.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.