Recursion: Linear and Tree Recursive Processes and Iteration CMSC 11500 Introduction to Computer Programming October 7, 2002.

Slides:



Advertisements
Similar presentations
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Advertisements

Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
Dynamic Programming.
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
CMPS1371 Introduction to Computing for Engineers SORTING.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. 2 Outline Scoping and block structure Recursive and iterative processes Orders of growth.
6.001 SICP – September Introduction
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. Reminder: Recursive algorithm Reduce problem to one or more sub-problems of smaller sizes (linear or tree.
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
מבוא מורחב 1 Lecture 3 Material in the textbook on Pages of 2nd Edition Sections to
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
Structure and Interpretation of Computer Programs Presented by Yan Yan CSE 294, UCSD, April 13, Chapter Harold Abelson, Gerald and Julie.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
1 Lecture 16: Lists and vectors Binary search, Sorting.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
Fall 2008Programming Development Techniques 1 Topic 4 Orders of Growth September 2008.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Data Abstraction: Sets Binary Search Trees CMSC Introduction to Computer Programming October 30, 2002.
Cs1321 December 6, 2001 Review. What is computer science? What's an algorithm? Processes and programs Overview of some programming language concepts Functional.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
From Lambda Calculus to LISP Functional Programming Academic Year Alessandro Cimatti
Analyzing Programs: Order of Growth CMSC Introduction to Computer Programming October 11, 2002.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
CS314 – Section 5 Recitation 9
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
CS314 – Section 5 Recitation 10
Data Abstraction: Sets
Edited by Original material by Eric Grimson
CS 550 Programming Languages Jeremy Johnson
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Decrease-and-Conquer Approach
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Closures and Streams cs784(Prasad) L11Clos
CS 270 Math Foundations of CS Jeremy Johnson
6.001 SICP Data abstractions
CS 3343: Analysis of Algorithms
This Lecture Substitution model
Abstraction and Repetition
Lecture #7 מבוא מורחב.
List and list operations (continue).
Defining Functions with DEFUN
Abstraction and Repetition
Today’s topics Abstractions Procedural Data
Divide & Conquer Algorithms
Lecture 13: Assignment and the Environment Model (EM)
This Lecture Substitution model
Abstraction and Repetition
Presentation transcript:

Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002

Roadmap Recap: Lists and Structural Recursion Structural Recursion (cont’d) –Returning lists from procedures –Hierarchical lists: lists of lists –Tree-structured Recursion Generative recursion (Problem-based) –Linear recursive processes –Iteration Order of growth & exponentiation

Recap: Lists: Self-referential data Recap: Lists and Structural Recursion –Arbitrarily long data structures Lists: Constructor: cons; Selectors: car, cdr Data definition: 1) empty list ‘() 2) (cons x alist) –Self-referential definition –Manipulating lists: Structural Recursion Self-referential procedures Need cases for: –1) Empty list –2) Compound case: Consume first (car) value; Call function on rest of list (cdr)

Returning Lists: Square-list Description: Take a list of numbers as input, return a list of numbers with each of the values squared (define (square-list numlist) (cond ((null? numlist) ‘()) (else (cons (square (car numlist)) (square-list (cdr numlist))))))

Lists of Lists Can create lists whose elements are lists –“closure” property of cons Objects formed by cons can be combined with cons –Allows creation of hierarchical data structures (cons (list 1 2) (list 3 4)) => ((1 2) 3 4) 12 34

Tree Recursion Before: Assumed all lists “flat” Now: Allow possibility of lists of lists (define (square-tree alist) (cond ((null? alist) ‘()) ((not (pair? alist)) (square alist)) (else (cons (square-tree (car alist)) (square-tree (cdr alist))))))

Generative Recursion Syntactically recursive: –Procedure calls itself Self-reference: –Property of solution to problem Rather than data structure of input –E.g. Factorial: n! = n*(n-1)*(n-2)*….*3*2*1 n! = n*(n-1)! –Function defined in terms of itself

Recursion Example: Factorial (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))) N=4 (* 4 (factorial 3)) (* 4 (* 3 (factorial 2))) (* 4 (* 3 (* 2 (factorial 1)))) (* 4 (* 3 (* 2 1)))) (* 4 (* 3 (* 2 1))) (* 4 (* 3 2)) (* 4 6) 24

Recursion Components Termination: –How you stop this thing???? –Condition& non-recursive step: e.g. (if (= n 1) 1) –Reduction in recursive step: e.g. (factorial (- n 1)) Compound and reduction steps –“Divide and conquer” Reduce problem to simpler pieces Solve pieces and combine solutions

Linear Recursive Processes How does the procedure use resources? –Time: How many steps? –Space: What do we have to remember Factorial: –Time: n multiplications –Space: Builds up deferred computations Interpreter must remember them Needs space for n remembered computations

Factorial: Iterative Process (define factorial (fact-iter 1 1 n)) (define fact-iter (product counter max-count) (if (> counter max-count) product (fact-iter (* counter product) (+ counter 1) max-count))) (fact-iter 1 1 4) (fact-iter 1 2 4) (fact-iter 2 3 4) (fact-iter 6 4 4) (fact-iter ) 24

Iterative Process Same computations as recursive factorial –Time: n multiplications –Space: constant: no deferred steps to remember Key: –State of computation captured by variables product, counter: how much has been done so far –If have state, could restart process