Download presentation
Presentation is loading. Please wait.
1
thirteen recursion
2
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]] ►
3
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]]]]]]]
4
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
5
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”)
6
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
7
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
8
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
9
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
10
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)
11
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)
12
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)
13
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)
14
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°
15
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
16
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
17
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]]]]]]]]
18
Level 0 and 1 snowflakes [snowflake 0][snowflake 1]
19
Level 2 and 3 snowflakes
20
Level 4 and 5 snowflakes Note: this has 3×4 5 = 3072 lines
21
Self-similarity: Zooming in on the level 5 snowflake
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.