Variable is used to hold the object. Any variable that
starts with capital letter is treated as variable in prolog.
Variable can be in either of below two states.
a.
Instantiated : Variable holds the object
b.
Not-instantiated : a variable holding is not
yet known.
Let me explain with an example.
animals.pl
bigger(elephant, fox). bigger(elephant, tiger). bigger(elephant, rabbit). bigger(elephant, ant). bigger(tiger, fox). bigger(tiger, rabbit). bigger(tiger, ant). bigger(fox, rabbit). bigger(fox, ant).
animals.pl file contains all the facts about the animals.
Let’s feed these facts to prolog system.
1 ?- consult(animals). true.
‘consult’ statement used to load the facts and rules.
How can we ask prolog
‘Give me some animal that is bigger than ant’?
You can ask using the statement ‘bigger(X, ant)’
2 ?- bigger(X, ant). X = elephant
In the above example, prolog reply to me by saying
elephant is bigger than ant. Variable X holds the object elephant.
If you are satisfied with the given answer, then press
‘.’, else enter ;, then prolog gives you the next possible match.
2 ?- bigger(X, ant). X = elephant . 3 ?- bigger(X, ant). X = elephant ; X = tiger ; X = fox.
Similarly, below snippet return the smaller elements than
elephant.
5 ?- bigger(elephant, Y). Y = fox . 6 ?- bigger(elephant, Y). Y = fox ; Y = tiger ; Y = rabbit ; Y = ant.
No comments:
Post a Comment