Presentation is loading. Please wait.

Presentation is loading. Please wait.

LING 388: Language and Computers Sandiway Fong Lecture 6: 9/7.

Similar presentations


Presentation on theme: "LING 388: Language and Computers Sandiway Fong Lecture 6: 9/7."— Presentation transcript:

1 LING 388: Language and Computers Sandiway Fong Lecture 6: 9/7

2 2 Administrivia Homework 2 –out today –usual rules due next Thursday by midnight

3 3 Last Time Building and Taking Atoms Apart built-in predicate atom_chars/2 has two modes of usage –atom_chars(symbol,list) 1.takes names apart –?- atom_chars(will,X). –X=[w,i,l,l] 2.builds names from a list of characters –?- atom_chars(X,[ ’ J’,o,h,n]). X=‘John’ Building and Taking Lists Apart append/3 is a built-in predicate in SWI-Prolog defined as follows: –append(L1,L2,L3) is true –if list L3 is the linear concatenation of lists L1 and L2 1.concatenate two lists together –?- append([1],[2,3],X). –X = [1,2,3] 2.split a list up into two parts –?- append(X,Y,[1,2]). –X=[] Y=[1,2] –X=[1] Y=[2] –X=[1,2] Y=[]

4 4 Exercise 1 Recursive Definition –Base Case –Recursive Case [member/2 is already taken in SWI-Prolog, let’s us mem/2.] Let’s define mem/2 such that –?- mem(List,X). is true if X is a member of List Examples: –?- mem([1,2,3],2). true –?- mem([1,2,3],4). false

5 5 Exercise 1 Let’s define mem/2 such that –?- mem(List,X). is true if X is a member of List Examples: –?- mem([1,2,3],2). true –?- mem([1,2,3],4). false Recursive Definition –Base Case mem([X|_],X). X is a member of a list is X is the head of that list (tail = _ ) –Recursive Case mem([_|L],X):- mem(L,X). X is a member of a list [_|L] if X is a member of its tail L

6 6 Exercise 1 Put the definition of mem/2 in a file and load it into Prolog: –mem([X|_],X). –mem([_|L],X):- mem(L,X). test to see if it’s loaded properly using –?- listing. Run the following queries: ?- mem([1,2,3],2). ?- mem([],2). ?- mem([1,2,3],X). what answer(s) does this last query give?

7 7 Homework Question 1 (6pts) Give the complete (i.e. all answers) step-by-step computation tree for ?- mem([1,2,3],X). given the database –mem([X|_],X). –mem([_|L],X):- mem(L,X). Hint: using –?- trace. –will help but you will need to list out the matches and variable binding at each step –see Lecture 5 slides for app/3 to see what format you should use

8 8 Exercise 2 Modal auxiliary verbs in English generally have contracted negative counterparts –shouldshouldn’t –wouldwouldn’t –couldcouldn’t –may*mayn’t –[* indicates ungrammaticality] Let’s write a rule to form the contracted negated form –call the predicate addNT/2 ?- addNT(Word,WordNT). Word = symbol WordNT = symbol with n’t suffixed –Examples: ?- addNT(should,X). X = ’shouldn\’t’

9 9 Exercise 2 We can use atom_chars/2 –atom_chars(symbol,list) 1.takes names apart –?- atom_chars(will,X). –X=[w,i,l,l] 2.builds names from a list of characters –?- atom_chars(X,[ ’ J’,o,h,n ]). X=‘John’ Definition (rule): addNT(W,Wnt) :- atom_chars(W,L), append(L,[n,’\’’,t],Lnt), atom_chars(Wnt,Lnt). add this rule to the database ?- listing. addNT(A, C) :- atom_chars(A, B), append(B, [n, '\'', t], D), atom_chars(C, D). run queries ?- addNT(should,X). ?- addNT(would,X). ?- addNT(john,X).

10 10 Homework Question 2 Assume database includes facts: modal(should). “should is a modal” modal(would). “would is a modal” modal(could). “could is a modal” modal(may). “may is a modal” (4pts) Modify the definition of addNT/2 to accept only modals Demonstrate your program works correctly for: ?- addNT(should,X). ?- addNT(would,X). ?- addNT(john,X). Submit both your program and queries as your answer –put everything together, –not in separate files!

11 11 Homework Question 2 (4pts) Further modify your definition of addNT/2 to exclude the ungrammatical case: –i.e. ?- addNT(may,X). No –shouldshouldn’t –wouldwouldn’t –couldcouldn’t –may*mayn’t

12 12 Homework Question 2 (6pts) Extra Credit Question Notice that the following query doesn’t work: ?- addNT(X,'shouldn\'t'). ERROR: atom_chars/2: Arguments are not sufficiently instantiated Write the corresponding “subtract n’t” rule, call it subNT/2, for removing the n’t suffix: ?- addNT(X,'shouldn\'t'). X = should

13 13 Exercise 3 Let’s define a recursive predicate for reversing lists Examples: [1,2][2,1] [1,2,3] [3,2,1][1][] Definition reverse([],[]). reverse([X|L],R) :- reverse(L,LR), append(LR,[X],R). Run queries ?- reverse([1,2,3],X). ?- reverse(X,[1,2,3]). what happens when you ask for more answers?

14 14 Homework Question 3 (6pts) Define a predicate pallindrome/1 –that is true when a word can be spelt the same forwards or backwards Examples: –radar –redivider –abba Demonstrate your program works on queries like: –?- pallindrome(radar). –Yes –?- pallindrome(dog). –No Hint: use atom_chars and reverse


Download ppt "LING 388: Language and Computers Sandiway Fong Lecture 6: 9/7."

Similar presentations


Ads by Google