Recursive definitions. Capiche? Less is more Recursive Definitions 1. Specify a function at its lowest/minimum level (zero? One? Empty?) 2. Give a rule.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

Artificial Intelligence: Natural Language and Prolog
Chapter 3 Regular Expression and Regular Languages
What two numbers will give you a product of 64 and a quotient of 4?
Combining Like Terms. Only combine terms that are exactly the same!! Whats the same mean? –If numbers have a variable, then you can combine only ones.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
What two numbers will give you a product of 64 and a quotient of 4?
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Formal Models of Computation Part II The Logic Model
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
CSC 270 Nov. 22, 2005 Last Day of Scheme Dr. Stephen Bloch
Haskell user defined types data Temp = Cold|Hot|Warm deriving (Show,Eq, Ord, Enum) -- to enable printing to screen -- comparing for equality -- comparison.
Introduction A function is called higher-order if it takes a function as an argument or returns a function as a result. twice :: (a  a)  a  a twice.
Discrete Math Recurrence Relations 1.
Computing functions with Turing machines
Modern Programming Languages, 2nd ed.
ABC Technology Project
Writing LISP functions. 2 COND Rule 1: Unless the function is extremely simple, begin with a COND If you can write the function body in one line, do it.
Jessie Zhao Course page: 1.
Addition 1’s to 20.
25 seconds left…...
4.1 Powers of 10.
Test B, 100 Subtraction Facts
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Finite-state Recognizers
Copyright © Cengage Learning. All rights reserved.
Week 1.
Datorteknik IntegerAddSub bild 1 Integer arithmetic Depends what you mean by "integer" Assume at 3-bit string. –Then we define zero = 000 one = 001 Use.
Less Than Matching Orgad Keller.
Starting Out with Java: From Control Structures through Objects
Introduction to Recursion and Recursive Algorithms
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Lecture 12 Recursion part 1
Primitive Recursive Functions (Chapter 3)
Recursive Definitions and Structural Induction
Induction and Recursion. Odd Powers Are Odd Fact: If m is odd and n is odd, then nm is odd. Proposition: for an odd number m, m k is odd for all non-negative.
CSE115/ENGR160 Discrete Mathematics 04/03/12 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 03/31/11
1 Section 3.4 Recursive Definitions. 2 Recursion Recursion is the process of defining an object in terms of itself Technique can be used to define sequences,
Discrete Mathematics CS 2610 March 26, 2009 Skip: structural induction generalized induction Skip section 4.5.
Induction and recursion
Chapter 4: Induction and Recursion
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
1 Languages. 2 A language is a set of strings String: A sequence of letters Examples: “cat”, “dog”, “house”, … Defined over an alphabet:
4.3 Recursive Definitions and Structural Induction Sometimes it is difficult to define an object explicitly. However, it may be easy to define this object.
Sequences and Summations Section 2.4. Section Summary Sequences. – Examples: Geometric Progression, Arithmetic Progression Recurrence Relations – Example:
1 Lecture 3.3: Recursion CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren, Zeph Grunschlag.
Discrete Structure Li Tak Sing( 李德成 ) Lecture 13 1.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
(Proof By) Induction Recursion
PPL Lazy Lists.
(defmacro (delay x) (list 'lambda () x))
A lightening tour in 45 minutes
Applied Discrete Mathematics Week 9: Integer Properties
Recursion.
Presentation transcript:

Recursive definitions

Capiche?

Less is more

Recursive Definitions 1. Specify a function at its lowest/minimum level (zero? One? Empty?) 2. Give a rule for finding a value with respect to a smaller value Sometimes called an inductive definition - a base step such as f(0) - an inductive step f(n+1) defined in terms of f(n) - an inductive step f(n) defined in terms of f(n-1) - scream downhill A typical example is factorial

Example fib(n) = fib(n-1) + fib(n-2) fib(0) = 0 fib(1) = 1 fib(4) = fib(3) + fib(2) = fib(2) + fib(1) + fib(2) = fib(1) + fib(0) + fib(1) + fib(2) = fib(1) + fib(2) = fib(2) = fib(1) + fib(0) = = 3 Fibonacci

Exercise find f(1), f(2), f(3), f(4) and f(5), where 1. f(0) = 3, f(n+1) = -2.f(n) alternatively f(n) = -2.f(n - 1) 2. f(0) = 3, f(n+1) = 3.f(n) + 7 alternatively f(n) = 3.f(n - 1) + 7

Example Define arithmetic on positive integers using only - isZero(x) : x == 0 - succ(x) : x pred(x) : x add(n,m) : ? - mult(n,m) : ? - pow(n,m) : ? - pow2(n) : ? Do more with less

Example Define arithmetic on positive integers using only - isZero(x) : x == 0 - succ(x) : x pred(x) : x add(n,m) : ? - mult(n,m) : ? - pow(n,m) : ? - pow2(n) : ? Do more with less Example/Intuition

Example Define arithmetic on positive integers using only - isZero(x) : x == 0 - succ(x) : x pred(x) : x add(n,m) : if isZero(n) then m else add(pred(n),succ(m)) - mult(n,m) : - pow(n,m) : ? - pow2(n) : ? Do more with less

Example Define arithmetic on positive integers using only - isZero(x) : x == 0 - succ(x) : x pred(x) : x add(n,m) : if isZero(n) then m else add(pred(n),succ(m)) - mult(n,m) : ? - pow(n,m) : ? - pow2(n) : ? Do more with less So, mult(n,m) might generate m additions of n? Intuition

Example Define arithmetic on positive integers using only - isZero(x) : x == 0 - succ(x) : x pred(x) : x add(n,m) : if isZero(n) then m else add(pred(n),succ(m)) - mult(n,m) : if isZero(m) then 0 else add(n,mult(n,pred(m))) - pow(n,m) : ? - pow2(n) : ? Do more with less

Example Define arithmetic on positive integers using only - isZero(x) : x == 0 - succ(x) : x pred(x) : x add(n,m) : if isZero(n) then m else add(pred(n),succ(m)) - mult(n,m) : ? - pow(n,m) : ? - pow2(n) : ? Do more with less NOTE: Ive assumed n and m are +ve and that m > 0

Recursion Try and define pow(n,m) and pow2(n) Factorial, try and define it recursively

Recursion What have we done? recursively defined functions functions have a base case (when one argument is 1 or 0 or something) functions defined in terms of previous function values we have to make sure our recursion stops!!!

Recursive definition of a set

Positive integers divisible by 3 We say whats in the set to start with then how to construct new elements of the set, depending on whats already in the set! Notice x and y could both be the same element! In fact, initially they must be!

Recursive definition of a set Positive integers congruent to 2 modulo 3 S = {2,5,8,11,14,17,20,…}

Exercise Give a recursive definition of the set of positive integers that are not divisible by 5

Exercise Give a recursive definition of the set of positive integers that are not divisible by 5 S = {1,2,3,4,6,7,8,9,11,12,13,14,16,…}

Recursively defined structures

This can be recursively defined as follows The empty string (thats lambda) is in the set We can take some string from the set of strings and add a new character from the alphabet to the end of it

Recursively defined structures First application generates 0 and 1 Second application generates 00, 01, 10, and 11 Third application generates 000,001,010,…,111 …

Recursively defined structures Example, concatenation of strings cat(abcd,hjg) = abcdhjg cat(af2,) = af2 cat(,) =

Recursively defined structures Example, length of a string l(abcd) = 4 l(af2) = 3 cat() = 0 Note: second step is just pattern matching, i.e. breaking up a string into a substring and its last character

Recursion over lists Assume we have a list (e1,e2,e3,…,en), i.e. a list of elements Examples: (ted,poppy,alice,nelson) (1,2,3,1,2,3,4,2,9) (please,turn,the,lights,off) (1,dog,cat,24,crock,crock,crock) Assume also we have the functions head(l) and tail(l) Examples: l=(1,2,3,1,2,3,1); head(l) = 1; tail(l) = (2,3,1,2,3,1) l =(dog); head(l) = dog; tail(l) = () l=(); head(l) = crash!; tail(l) = crash!

Recursion over lists

Also cons(e,l) to construct a new list with element e as head and l as tail Examples: cons(cat,(dog,bird,fish)) = (cat,dog,bird,fish) cons(3,()) = (3) cons(5,(4,5,4,5)) = (5,4,5,4,5)

Well formed formulae (wff) Pronounced wiff

wff Well formed formulae for the predicate calculus can be defined using T and F, propositional variables, and the operators (not, or, and, implies, biconditional)

fin