home—lectures—recipe—hws—exams—D2L—zoom (snow day)
Prolog: recursive relations
We will again (in class) use swi-prolog.org's
online interpreter.
If you're interested in a different
explanation/view, here is another prolog tutorial: www.doc.gold.ac.uk/~mas02gw/prolog_tutorial/prologpages/
You can also run /usr/local/bin/xsb from rucs.
In XSB,
you can load a file family.pl from the xsb prompt, by typing:
[family].
warning: Don't include spaces or hyphens in your filename.
xsb interprets hyphens as subtraction,
spaces as a list with two objects,
and gives a syntax error.
We will use
family-before.pl for the following
(resulting in family-after.pl):
- Review: facts vs rules
- 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—hws—exams—D2L—zoom (snow day)
 This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland radford.edu |
 |