03.06.2013 Views

Turbo Prolog

Turbo Prolog

Turbo Prolog

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Program 7 is a complete <strong>Turbo</strong> <strong>Prolog</strong> program that functions as a mini telephone<br />

directory that uses the standard predicates readln and write. The domains section has<br />

been omitted, since only standard domains are used. The program asks for a name to<br />

be typed in. When the name is entered, the corresponding telephone number is found<br />

from the database and displayed on the screen.<br />

1* Program 7 *1<br />

predicates<br />

reference(symbol,symbol)<br />

goal<br />

write(IIPlease type a name :11),<br />

readln(The_Name),<br />

reference(The_Name,Phone_No),<br />

write (liThe phone number is II, Phone_No) , nl.<br />

clauses<br />

reference(IIAlbertll , 1101-1231;56 11 ).<br />

reference(IIBettyll, 1101-569767 11 ).<br />

reference(IICarolll , 1101-2671;00 11 ).<br />

reference(IIDorothyll,1I01-191051 11 ).<br />

Finally, to illustrate the char domain type, Program 8 defines is/etter which, when given<br />

the goals<br />

isletter( '%').<br />

isletter( 'Q').<br />

will return false and true respectively.<br />

1* Program 8 *1<br />

predicates<br />

isletter(char)<br />

clauses<br />

isletter(Ch) if Ch (= 'z' and 'a' (= Ch.<br />

isletter(Ch) if Ch (= 'Z' and 'A' (= Ch.<br />

Exercise Type in Program 7 and try each of these goals in turn.<br />

(1) reference (IiCarol ll , y) .<br />

(2) reference(X,1I01-191951 11 ).<br />

(3) reference(IIMavisll,y).<br />

(1;) reference(X,y).<br />

Kim shares a flat with Dorothy and so has the same phone number. Add this information<br />

to the clauses for the predicate reference and try the goal<br />

reference(X,1I01-191051 11 ).<br />

to check your addition.<br />

Type Program 8 and try each of these goals in turn.<br />

(1) isletter('x').<br />

(2) isletter('2').<br />

(3) isletter(lIhello ll ).<br />

(1;) isletter(a).<br />

(5) isletter(X).<br />

Tutorial II: A Closer Look at Domains, Objects and Lists 37

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!