Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 2 Administrivia Reminder –Homework 3 –due next Monday by midnight

3 Today’s Topic Practice makes perfect… –more practice writing programs

4 Natural Numbers (NN) Infinite set: –N = {1,2,3,4,5,..} Base case nn(1). –“1 is a natural number” Recursive case nn(N) :- nn(M), N is M+1. –“N is a natural number if M is a natural number and N is M+1” ?- nn(X). X = 1 ; X = 2 ; X = 3 ; X = 4 ; X = 5 ; X = 6 ; X = 7 ; X = 8 ; X = 9

5 Even Numbers Suppose –even numbers are natural numbers that are divisible by 2 Set: –N = {1,2,3,4,5,..} –E = {2,4,..} Task: –Write a predicate even/1 that generates the even numbers Method 1 Base Case even(2). Recursive Case even(N) :- even(M), N is M+2. Query: ?- e(X). X = 2 ; X = 4 ; X = 6 ; X = 8

6 Even Numbers Suppose –even numbers are natural numbers that are divisible by 2 Set: –N = {1,2,3,4,5,..} –E = {2,4,..} Task: –Write a predicate even/1 that generates the even numbers Method 2 Use the natural number definition and permit only the even ones Modify: nn(1). nn(N) :- nn(M), N is M+1. into even(N) :- nn(N), is_even(N). where is_even(N) tests N to see if it’s an even number

7 Even Numbers Method 2 Use the natural number definition and permit only the even ones Modify: nn(1). nn(N) :- nn(M), N is M+1. into even(N) :- nn(N), is_even(N). where is_even(N) tests N to see if it’s an even number Suppose –even numbers are natural numbers that are divisible by 2 Let’s use Prolog’s built-in arithmetic evaluation predicate is/2 –?- Y is 3/2. produces a floating point number –Y=1.5 –?- Y is 4/2. produces an integer – Y=2 –?- Y is round(3/2). rounds up to the nearest integer – Y=2

8 Even Numbers Suppose –even numbers are natural numbers that are divisible by 2 Let’s use Prolog’s built-in arithmetic evaluation predicate is/2 –?- Y is 3/2. produces a floating point number –Y=1.5 –?- Y is 4/2. produces an integer – Y=2 –?- Y is round(3/2). rounds up to the nearest integer – Y=2 Definition so far even(N) :- nn(N), is_even(N). where is_even(N) tests N to see if it’s an even number Simple Idea: –to test whether a number X is even 1.divide it by 2, call this number Y 2.divide it by 2 and round it, call this number Z 3.X is even if Y=Z. Define is_even(X) :- Y is X/2, Z is round(X/2), Y = Z.

9 Odd Numbers Even numbers Idea: –to test whether a number X is even 1.divide it by 2, call this number Y 2.divide it by 2 and round it, call this number Z 3.X is even if Y=Z. Define is_even(X) :- Y is X/2, Z is round(X/2), Y = Z. Odd numbers Define is_odd(X) :- Y is X/2, Z is round(X/2), \+ Y = Z. or define is_odd(X) :- \+ is_even(X).

10 Output You can use built-in predicates –write/1 and –nl/1 to print values of variables etc. Example –if X = the ?- write(X), nl. –prints the [newline] Sometimes useful for debugging is_odd(X) :- Y is X/2, write(Y), nl, Z is round(X/2), write(Z), nl, \+ Y = Z. Examples: –?- is_odd(7). 3.5 4 Yes –?- is_odd(6). 3 No

11 If-Then-Else Template: – –-> –; – are all Prolog goals (queries) Example: test(N) :- is_even(N) -> write(N), write(‘ is even’), nl ; write(N), write(‘ is odd’), nl. Queries: ?- test(3). 3 is odd Yes ?- test(4). 4 is even Yes

12 An Aside http://en.wikipedia.org/wiki/Parity_of_zero Zero is considered to be an even number –Integer multiple of 2 (definition) shares all properties of even numbers

13 Nested If-Then-Else Template: – –-> –; ( – -> – ; ) – are all Prolog goals (queries) –(... ) indicate nesting Example: test(N) :- N = 0 -> write(‘Zero is tricky’), nl ; (is_even(N) -> write(N), write(‘ is even’), nl ; write(N), write(‘ is odd’), nl). Queries: ?- test(0). Zero is tricky

14 Exercise (Ungraded homework exercise) Write a Prolog program to enumerate the integer line Where would you start? i.e. a program that would print out all and only the numbers on the integer line (given enough time…)


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

Similar presentations


Ads by Google