Download presentation
Presentation is loading. Please wait.
1
LING 388: Language and Computers Sandiway Fong Lecture 7: 9/12
2
2 Administrivia No class (this) Thursday Homework 2 –due Thursday by midnight –submit your homework by email
3
3 Today’s Topic More practice with writing Prolog programs
4
4 Natural Numbers 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
5 Even Numbers Definition: –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
6 Even Numbers Definition: –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
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 Definition: –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
8 Even Numbers Definition: –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 numberIdea: –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
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
10 Output You can use built-in predicates –write/1 and –nl/1 to print values of variables etc. Example –if X = the ?- write(the),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
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
12 Nested If-Then-Else Template: – –-> –; ( – -> – ; ) – are all Prolog goals (queries) –(... ) indicate nesting Example: test(N) :- N = 0 -> write(‘Zero’), nl ; (is_even(N) -> write(N), write(‘ is even’), nl ; write(N), write(‘ is odd’), nl). Queries: ?- test(0). Zero
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.