Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array.

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

C++ Programming:. Program Design Including
MATH 224 – Discrete Mathematics
Recursion.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.
Six compound procedures and higher-order procedures.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Eight compound procedures and higher-order procedures.
Copyright © 2003 Pearson Education, Inc. Slide 1.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
29-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Holt Geometry 12-Ext Using Patterns to Generate Fractals 12-Ext Using Patterns to Generate Fractals Holt Geometry Lesson Presentation Lesson Presentation.
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.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
"Clouds are not spheres, mountains are not cones, coastlines are not circles, and bark is not smooth, nor does lightning travel in a straight line."(Mandelbrot,
Chapter 9: Geometry.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Comp 249 Programming Methodology Chapter 10 – Recursion Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Graphics and Procedures Programming Right from the Start with Visual Basic.NET 1/e 5.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Recursion Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zWhat is a recursive function?
Recursion AP Computer Science A Mr. Langner By: Thomas Robbins.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering CS 241 Computer Programming II CS 241 – Computer Programming.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Fractals. What do we mean by dimension? Consider what happens when you divide a line segment in two on a figure. How many smaller versions do you get?
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
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.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
RECURSION.
Recursion CSC 202.
Recursion This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the instructor’s class materials.
This Lecture Substitution model
Basics of Recursion Programming with Recursion
Recursion Taken from notes by Dr. Neil Moore
Fundaments of Game Design
Solving Polynomials 1 Review of Previous Methods 2 Grouping.
This Lecture Substitution model
slides created by Marty Stepp
Lecture 6 - Recursion.
Presentation transcript:

thirteen recursion

Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array object spacing [− count 1]]]]]]] ► [group [box ] [horizontal-array [box 20 20] 30 6]] ►

Wait a minute … We’ve defined horizontal-array in terms of itself Isn’t that circular? Well, sort of … We’ve defined how to make an array of 3 objects in terms of an array of 2 objects We’ve defined how to make an array of 2 objects in terms of an array of 1 object We’ve defined how to make an array of 1 object directly [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array object spacing [− count 1]]]]]]]

Recursion Solving a problem using the solution to a simpler version of the problem Recursive procedures Call themselves with “simpler” values for the arguments For horizontal-array, this means count is smaller Use the result to compute the final result Recursion needs to terminate Can’t get lost infinite recursion (infinite regress) So before calling ourselves, we check to see if the problem is so simple we can code the solution directly

Schematic form of recursion [define recursive [args … → [if simple-problem? simple-answer [fix-up [recursive simpler-args]]]]] Every recursive procedure Checks for simpler version(s) of the problem Calls itself with a simpler version of the args But some recursive procedures Have many checks for different simple versions of the problem Call themselves more than once (“tree recursion”) Just return the result of the recursive call without fix-up (“iteration”)

Data as hierarchy When we group objects inside groups inside other groups We get a tree- structured hierarchy Kind of like outlines And kind of like code group translatebox group translatebox group translatebox

The snowflake fractal A recursive shape Three sides formed as follows: Start with a line Break the line into thirds Break the middle third and stretch it Stretch until you have 4 equal pieces Now repeat the process on each piece Keep going forever

The snowflake in meta How do we draw one of these “lines” Can’t really draw an infinite number of lines So we’ll only repeat the dividing process a finite number of times Write a procedure that takes the number of times to divide as an argument Draws a simple line if it’s zero (times to divide) Otherwise Makes four copies of the line divided n-1 times (by recursing) Arranges them in the right places

The with expression [with name = exp … body] Finds value of exp (once) Substitutes value of exp in for every occurrence of name within body Evaluates body and returns its value Just a nice way of avoiding typing something repeatedly

The snowflake in meta [define snowflake-line [level → [if [= level 0] «End of recursion – just draw a line» [line [point 0 0] [point 9 0]] «Otherwise keep recursing» [with subline = [scale [/ 1 3] [snowflake-line [- level 1]]] [group subline [translate [point 3 0] [rotate -60 subline [translate [point 3 0] [rotate 120 subline]]]] [translate [point 6 0] subline]]]]]] (0,0)(3,0)(6,0)

First, the easy (non-recursive) case [define snowflake-line [level → [if [= level 0] «End of recursion – just draw a line» [line [point 0 0] [point 9 0]] «Otherwise keep recursing» [with subline = [scale [/ 1 3] [snowflake-line [- level 1]]] [group subline [translate [point 3 0] [rotate -60 subline [translate [point 3 0] [rotate 120 subline]]]] [translate [point 6 0] subline]]]]]] (0,0)(3,0)(6,0)

The hard case [define snowflake-line [level → [if [= level 0] «End of recursion – just draw a line» [line [point 0 0] [point 9 0]] «Otherwise keep recursing» [with subline = [scale [/ 1 3] [snowflake-line [- level 1]]] [group subline [translate [point 3 0] [rotate -60 subline [translate [point 3 0] [rotate 120 subline]]]] [translate [point 6 0] subline]]]]]] (0,0)(3,0)(6,0)

Segment 1 [define snowflake-line [level → [if [= level 0] «End of recursion – just draw a line» [line [point 0 0] [point 9 0]] «Otherwise keep recursing» [with subline = [scale [/ 1 3] [snowflake-line [- level 1]]] [group subline [translate [point 3 0] [rotate -60 subline [translate [point 3 0] [rotate 120 subline]]]] [translate [point 6 0] subline]]]]]] (0,0)(3,0)(6,0)

Segment 2 [define snowflake-line [level → [if [= level 0] «End of recursion – just draw a line» [line [point 0 0] [point 9 0]] «Otherwise keep recursing» [with subline = [scale [/ 1 3] [snowflake-line [- level 1]]] [group subline [translate [point 3 0] [rotate -60 subline [translate [point 3 0] [rotate 120 subline]]]] [translate [point 6 0] subline]]]]]] (0,0)(3,0)(6,0) shift 3 pixels and rotate 60°

Segment 3 (very confusing, but trust me) [define snowflake-line [level → [if [= level 0] «End of recursion – just draw a line» [line [point 0 0] [point 9 0]] «Otherwise keep recursing» [with subline = [scale [/ 1 3] [snowflake-line [- level 1]]] [group subline [translate [point 3 0] [rotate -60 subline [translate [point 3 0] [rotate 120 subline]]]] [translate [point 6 0] subline]]]]]] (0,0)(3,0)(6,0) shift 3 more pixels and rotate120° back

Segment 4 [define snowflake-line [level → [if [= level 0] «End of recursion – just draw a line» [line [point 0 0] [point 9 0]] «Otherwise keep recursing» [with subline = [scale [/ 1 3] [snowflake-line [- level 1]]] [group subline [translate [point 3 0] [rotate -60 subline [translate [point 3 0] [rotate 120 subline]]]] [translate [point 6 0] subline]]]]]] (0,0)(3,0)(6,0) shift 6 pixels from the original position

Making the final snowflake [define snowflake [level → [with line = [snowflake-line level] [rotate -60 line [translate [point 9 0] [rotate 120 line [translate [point 9 0] [rotate 120 line]]]]]]]]

Level 0 and 1 snowflakes [snowflake 0][snowflake 1]

Level 2 and 3 snowflakes

Level 4 and 5 snowflakes Note: this has 3×4 5 = 3072 lines

Self-similarity: Zooming in on the level 5 snowflake