Variables in prolog starts with capital letter (or)
underscore symbol.
Example
X, Parent, Person1 are variables.
relations.pl
male(krishna). male(rama). male(hari). female(chamu). female(sowmya). female(sailaja). female(sridevi). parents(krishna, devi, lakshman). parents(rama, devi, lakshman). parents(hari, sudha, jangayya). parents(chamu, lakshmi, narayana). parents(sowmya, harini, ramakrishna). parents(sailaja, nhargavi, narayana). parents(sridevi, lakshmi, narayana). brothers(Person1, Person2) :- male(Person1), male(Person2), parents(Person1, Mother, Father), parents(Person2, Mother, Father). sisters(Person1, Person2) :- female(Person1), female(Person2), parents(Person1, Mother, Father), parents(Person2, Mother, Father).
In the above example, Person1, Person2, Mother and Father
are variables.
Anonymous variable
Some times, you may need to use a variable, but name may
not be used. In that case, you can use anonymous variable. Anonymous variable
is defined using _ symbol.
For example,
relations.pl
male(krishna). male(rama). male(hari). female(chamu). female(sowmya). female(sailaja). female(sridevi). parents(krishna, devi, lakshman). parents(rama, devi, lakshman). parents(hari, sudha, jangayya). parents(chamu, lakshmi, narayana). parents(sowmya, harini, ramakrishna). parents(sailaja, nhargavi, narayana). parents(sridevi, lakshmi, narayana). brothers(Person1, Person2) :- male(Person1), male(Person2), parents(Person1, Mother, Father), parents(Person2, Mother, Father). sisters(Person1, Person2) :- female(Person1), female(Person2), parents(Person1, Mother, Father), parents(Person2, Mother, Father).
If you want to check, is Krishna has any brothers? You
can use below statement.
brothers(_, krishna).
_ is replaced with an occurrence of a distinct variable .
No comments:
Post a Comment