Variable Example I (Cont.)
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, not john or fred. */
?- loves( fred, Who ). /* Who does fred love? */
Who = hobbies /* Who is just a variable name */
yes /* and it unifies with hobbies. */
Variable Example II
Here are some more difficult object/variable comparisons.
Consider the following database showing a library of cassette tapes.
Notice the final argument to tape
, which is the name of a favourite song from the album.
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