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

Slides:



Advertisements
Similar presentations
LING 388: Language and Computers
Advertisements

LING/C SC/PSYC 438/538 Lecture 11 Sandiway Fong. Administrivia Homework 3 graded.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 13: 10/9.
LING 388: Language and Computers Sandiway Fong Lecture 9: 9/27.
LING 388: Language and Computers Sandiway Fong 9/29 Lecture 11.
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/24.
LING 388: Language and Computers Sandiway Fong Lecture 15.
Consecutive Integer Problems. What is a consecutive integer? Integer: natural numbers and zero Integer: natural numbers and zero Consecutive: one after.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 3: 8/29.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/12.
LING/C SC/PSYC 438/538 Lecture 4 9/1 Sandiway Fong.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 9: 9/21.
LING 388: Language and Computers Sandiway Fong Lecture 12: 10/5.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/11.
LING 388: Language and Computers Sandiway Fong Lecture 28: 12/6.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/6.
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/7.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 11: 10/3.
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/13.
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
LING 388: Language and Computers Sandiway Fong Lecture 11: 10/3.
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/29.
LING 388 Language and Computers Take-Home Final Examination 12/9/03 Sandiway FONG.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 12: 10/5.
LING 388 Language and Computers Lecture 18 10/30/03 Sandiway FONG.
LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.
Computational Intelligence 696i Language Lecture 1 Sandiway Fong.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 17: 10/24.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 5: 9/5.
1 LING 438/538 Computational Linguistics Sandiway Fong Lecture 4: 8/31.
LING 388: Language and Computers Sandiway Fong Lecture 13: 10/10.
LING 388 Language and Computers Lecture 6 9/18/03 Sandiway FONG.
LING 388 Language and Computers Lecture 15 10/21/03 Sandiway FONG.
LING 388: Language and Computers Sandiway Fong Lecture 8.
LING 388: Language and Computers Sandiway Fong 10/4 Lecture 12.
Sect. 7.1 Radical Expressions & Radical Functions  Square Roots The Principal Square Root  Square Roots of Expressions with Variables  The Square Root.
7.1 Synthetic Division & the Remainder & Factor Theorems
LING 388: Language and Computers Sandiway Fong Lecture 11.
LING 408/508: Programming for Linguists Lecture 19 November 4 th.
LING 388: Language and Computers Sandiway Fong Lecture 5.
LING 388: Language and Computers Sandiway Fong Lecture 4.
LING 388: Language and Computers Sandiway Fong Lecture 7.
1.5 Divisibility Rules SRB pg 11..
LING 388: Language and Computers Sandiway Fong Lecture 6.
Michaelmas Term 2004 Discrete Mathematics CSC 141 Discrete Mathematics Dr. Corina Sas and Ms. Nelly Bencomo
LING 388: Language and Computers Sandiway Fong Lecture 7 9/15.
LING 388: Language and Computers Sandiway Fong Lecture 10.
Lesson 1.7 Dividing Integers Standards: NS 1.2 and AF 2.1 Objectives: Dividing Integers Evaluate variable expressions.
LING 388: Language and Computers Sandiway Fong Lecture 13.
LING/C SC/PSYC 438/538 Lecture 12 10/4 Sandiway Fong.
8/29/2014BCHB Edwards Introduction to Python BCHB Lecture 2.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong. Administrivia Homework 2 graded.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
1 Warm-up Determine if the following are polynomial functions in one variable. If yes, find the LC and degree Given the following polynomial function,
LING 388: Language and Computers Sandiway Fong Lecture 25.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
LING 408/508: Programming for Linguists Lecture 20 November 16 th.
LING 388: Language and Computers Sandiway Fong Lecture 20.
1.8 Dividing Real Numbers Objectives To Divide Real Numbers To evaluate algebraic expressions involving multiplication and division.
Arithmetic operations & assignment statement
Direct Proof and Counterexample III
Multiplying Integers SAME SIGNS??? Answer will be POSITIVE. ex)
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
3.1 Functions Ex 1: Some real-life situations:
Warm Up Write the first 4 terms of each sequence:
Presentation transcript:

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

2 Administrivia No class (this) Thursday Homework 2 –due Thursday by midnight –submit your homework by

3 Today’s Topic More practice with writing Prolog programs

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 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 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 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 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 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(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) 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 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