eats(fred,mangoes).
How do we ask what fred eats. We could type in something like
?- eats(fred,what).
However Prolog will say no. The reason for this is that what does not
match with mangoes. In order to match arguments in this way we must use a
Variable. The process of matching items with variables is known as
unification. Variables are distinguished by starting with a capital
letter. Here are some examples:
X /* a capital letter */
VaRiAbLe /* a word - it be made up or either case of letters */
My_name /* we can link words together via '_' (underscore) */
Thus returning to our first question we can find out what fred eats by typing
?- eats(fred,What).
What=mangoes
yes
As a result of this query, the variable What has matched (or unified) with
mangoes. We say that the variable What now has the binding mangoes. When
we pose a query, if the query is successful, Prolog prints both the
variable and the variable name, as we see above.
loves(john,mary).
loves(fred,hobbies).
Now let's look at some simple queries using variables
?- loves(john,Who). /* Who does john love? */
Who=mary /* yes , Who gets bound to mary */
yes /* and the query succeeds*/
?- loves(arnold,Who) /* does arnold love anybody */
no /* no, arnold doesn't match john or fred */
?- loves(fred,Who). /* Who does fred love */
Who = hobbies /* Note the to Prolog Who is just the name of a variable, it */
yes /* semantic connotations are not picked up, hence Who unifies */
/* with hobbies */
tape(1,van_morrison,astral_weeks,madam_george).
tape(2,beatles,sgt_pepper,a_day_in_the_life).
tape(3,beatles,abbey_road,something).
tape(4,rolling_stones,sticky_fingers,brown_sugar).
tape(5,eagles,hotel_california,new_kid_in_town).
Let's now look at some queries.
?- tape(5,Artist,Album,Fave_Song). /* what are the contents of tape 5 */
Artist=eagles
Album=hotel_california
Fave_Song=new_kid_in_town
yes
?- tape(4,rolling_stones,sticky_fingers,Song). /* find just song */
Song=brown_sugar /* which you like best from the album */
yes