home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
prolog-recursion1
prolog intro
tutorials: www.learnprolognow.com
www.amzi.com/AdventureInProlog
You can also run jlog, a web interpreter: see hints on Running JLog
We saw already:
- run /usr/local/bin/xsb from rucs
- facts vs rules
-
To load a file family.P from the xsb prompt, type:
[family].
warning:
Don't include spaces or hyphens in your filename.
xsb interprets hyphens as subtraction, and gives a syntax error.
-
a standard family-tree example, with rules like “father(X,Y) :- male(X), parent(X,Y).”.
-
Self-exercise: grandfather, w/ a partner.
(What is the def'n in English, for "Old is a grandparent of Yng"?)
-
talk/reflect about how this is declarative programming
(just saying 'prolog searches for all solutions', without saying more);
-
write a recursive rule, “ancestor/2”.
(note that the “/2” is not part of the name per se;
it indicates that ancestor is a binary predicate —
so you'll say make queries like ancestor(bart,lisaJr).
-
have them take a crack at writing with a partner:
-
nth-father (generalized grandfather; call 'ancestor')
-
sameLastName (or, “sameYChromosone”): check for male-only lineage
-
Write cousin.
-
Digress, in English:
cousin, 2nd cousin, 3rd cousin;
nth cousin m times removed.
-
Write nthCousin (which detects exact nth-cousins, 0 times removed)
-
Remember facts just rules w/o variables;
no intrinsic difference inside prolog.
-
Helpful tips:
trace,
and see http://www.cs.toronto.edu/~joanis/324/w01/xsbprolog.txt.
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)