This version of Learn Prolog Now! embeds SWI SH , SWI-Prolog for SHaring. The current version rewrites the Learn Prolog Now! HTML on the fly, recognising source code and example queries. It is not yet good at recognising the relations between source code fragments and queries. Also Learn Prolog Now! needs some updating to be more compatible with SWI-Prolog. All sources are on GitHub:
LearnPrologNow LPN SWISH Proxy SWISH

8.4 Exercises

Exercise  8.1 Here’s our basic DCG:

s --> np,vp.

np --> det,n.

vp --> v,np.
vp --> v.

det --> [the].
det --> [a].

n --> [woman].
n --> [man].
n --> [apple].
n --> [pear].

v --> [eats].

Suppose we add the noun “men” (which is plural) and the verb “know”. Then we would want a DCG which says that “The men eat” is ok, “The man eats” is ok, “The men eats” is not ok, and “The man eat” is not ok. Change the DCG so that it correctly handles these sentences. Use an extra argument to cope with the singular/plural distinction.

Exercise  8.2 In the text, we only gave examples of DCG rules with one extra argument, but in fact you can add as many extra arguments as you like. Here’s a DCG rule with three extra arguments:

kanga(V,R,Q) --> roo(V,R),jumps(Q,Q),{marsupial(V,R,Q)}.

Translate it into the form Prolog uses.

eXTReMe Tracker
© 2006-2012 Patrick Blackburn, Johan Bos, Kristina Striegnitz