RECURSION CITS1001. 2 Scope of this lecture Concept of recursion Simple examples of recursion.
Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1]...
-
date post
19-Dec-2015 -
Category
Documents
-
view
220 -
download
0
Transcript of Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1]...
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 400 100] [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×45 = 3072 lines
Self-similarity:Zooming in on the level 5 snowflake