EXERCISE SHEET 1
{The idea with these exercises is that you look at what you're supposed to type, and see if you can guess what the result will be. Then you try it out and discover whether you were correct or not! My comments are in curly brackets, {like this}, and what you're expected to type is not, and is indented so you can see it easily.}
{This series of exercises revolves around a family tree. A file which contains the program will be distributed to you. So you don't have to type in yourself. You can open files and programs using any text editor. To run Prolog, just type:
1 |
|
we suggest yap, but you can also use swipl. {After some uninteresting information, you'll get the ?- prompt. You can now make Prolog read in the TREE file by typing:}
1 |
|
{The [
and ]
are important, the .
is VERY important, miss it out of
any of these examples and Prolog will wait patiently until you type it...
OK, now for a few easy exercises. Looking at the program listing, what
will happen if you type the following queries:}
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
{Yes, that last one was a deliberate msspelling... In the next batch,
answer all the X = blah
sort of results with a ;
(followed by a
return) so you get all the answers. Don't put the quotes round the
semi-colon, please... Also, don't worry if you were expecting X =
blah Y = blahblah
and get Y = blahblah X = blah
instead; what's
important is that X and Y are instantiated to the correct things - the
order they're reported in a particular answer doesn`t really matter.}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
{This next bit means you have to type in a couple of relations
yourself. The square brackets notation is used to read in files,
[user]
being the special case of reading from the keyboard. The ^D
at the end is a control-D, obtained by holding the control key down
and hitting the D. Notice that the prompt changes while you're typing
in the clauses. The \+
means not
. The \+
function succeeds if
it cannot find a match in the database, and fails if it can.}
1 2 3 4 |
|
{It`ll then say some rubbish about consulting user, and come up with the ?- prompt again (unless you made a typing mistake...). OK, now you can try these new functions out.}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
{Those last 2 may surprise you! Now for some more clauses, this time
I'm not actually going to tell you what they do, you're supposed to
guess... The \==
operator means not equal to
.}
1 2 3 4 5 |
|
{To test these out, you might like to try the following:}
1 2 3 4 5 6 7 8 9 10 11 |
|
{Using any of the functions defined so far (which will always be
assumed in these exercises) define for yourself the two functions
grandfather
and aunt
. To make it easy, I`ll give you the
definitions in English: G is the grandfather of M if P is the parent
of M and G is the father of P; A is the aunt of M if P is the parent
of M, F is the father of P, bil', for brother-in-law. There are
three ways someone can be your brother-in-law: they're your sister's
husband; they're your wife's brother; they're your husband's brother. Once
you have defined them, try out:}
1 |
|
{You should get frank's brothers-in-law as being stephen and ian. Now for another mystery function: what does 'd' do here?}
1 2 3 4 |
|
{Try it on:}
1 2 3 |
|
1 |
|
{See if you get all four.}
{That's the end of this exercise sheet, if you managed this far then get someone close by to pat you on t he back.}