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

Slides:



Advertisements
Similar presentations
THE WELL ORDERING PROPERTY Definition: Let B be a set of integers. An integer m is called a least element of B if m is an element of B, and for every x.
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 Lecture 15.
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 388 Language and Computers Lecture 19 11/04/03 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 8: 9/18.
Congruence class arithmetic. Definitions: a ≡ b mod m iff a mod m = b mod m. a  [b] iff a ≡ b mod m.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/11.
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 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 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.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 17: 10/24.
LING 388: Language and Computers Sandiway Fong Lecture 7: 9/12.
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 Sandiway Fong Lecture 8.
Sect. 7.1 Radical Expressions & Radical Functions  Square Roots The Principal Square Root  Square Roots of Expressions with Variables  The Square Root.
LING 388: Language and Computers Sandiway Fong Lecture 11.
LING 408/508: Programming for Linguists Lecture 19 November 4 th.
Lecture 9. Arithmetic and geometric series and mathematical induction
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.
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
Introduction to sequences and series
LING/C SC/PSYC 438/538 Lecture 7 9/15 Sandiway Fong.
Properties of Real Numbers. Sets In mathematics, a set is a collection of things Sets can be studies as a topic all on its own (known as set theory),
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
LING/C SC/PSYC 438/538 Lecture 12 10/4 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong. Administrivia Homework 2 graded.
LING 388: Language and Computers Sandiway Fong Lecture 12.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
LING 388: Language and Computers Sandiway Fong 9/27 Lecture 10.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.
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.
LING 388: Language and Computers Sandiway Fong Lecture 20.
9.1 Sequences and Series. Definition of Sequence  An ordered list of numbers  An infinite sequence is a function whose domain is the set of positive.
Lecture#15 Discrete Mathematics. Summation Computing Summation Let a 0 = 2, a 1 = 3, a 2 = -2, a 3 = 1 and a 4 = 0. Compute each of the summations: =
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Discrete Mathematics
Objective - To divide integers. Odd # of Negatives = Negative Even # of Negatives = Positive Simplify. 1) 2) 3) 4) 5) 6) 7) 8) (-2)
2.2 Solving Two- Step Equations. Solving Two Steps Equations 1. Use the Addition or Subtraction Property of Equality to get the term with a variable on.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
1.8 Dividing Real Numbers Objectives To Divide Real Numbers To evaluate algebraic expressions involving multiplication and division.
Multiplication and Division of Powers
Congruence class arithmetic
Arithmetic operations & assignment statement
Direct Proof and Counterexample III
Modular Arithmetic and Change of Base
CHAPTER 2 Expressions.
Direct Proof and Counterexample III
3.1 Functions Ex 1: Some real-life situations:
Presentation transcript:

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

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

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

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

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

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

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

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.

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

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) Yes –?- is_odd(6). 3 No

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

An Aside Zero is considered to be an even number –Integer multiple of 2 (definition) shares all properties of even numbers

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

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