Tip:
Highlight text to annotate it
X
Doctor Racket, intermediate student with lambda, and function Koch has a problem.
I have to type (Koch (-d 1)) four times in this version,
and that is not only tedious, but also a potential source of errors.
We can do better!
Here is a function Koch2, that produces the same fractal but in a slightly different way.
Let's look at the definition,
The first question/answer pair is identical to the old version,
So that's not going to be the source of innovation.
The second question/answer pair has this local definition,
Two local definitions inside of Koch2.
They can't be seen outside Koch2, and they're gonna do some extra work for us.
d-, is a constant definition that has the value of 1 less than d.
k is the definition of a function.
That function is just Koch2 of d- rotated whatever angle "angl" is.
What can we do with that?
Well with map we can use k to produce a list of Koch images,
rotated 0, 60, -60, and 0 degrees.
Then we can paste those 4 images together using apply and beside/align.
It might be a little hard to imagine, so let's look at a particular example
outside of Koch2 where I defined a function k2 to behave the same way that k would
in the case where d is 2, so it produces a Koch2 of d-1 or 1
and it will rotate it whatever angle it is told to rotate it.
Let's try mapping that over a few angles:
So if I map, k2 over a list of angles, let's say 0, 60, -60, and 0
Produces a new list of those 4 Koch images.
Now I can paste those together using Apply and Beside/Align.
And this builds up the next most complicated Koch fractal.
Alright, let's see if we can understand how Koch2 works. Koch2 of 0 to understand that
we go to the function definition, and we replace all of the d's by 0.
The first question says, "Is 0 equal to 0?" Sure it is! So it produces that familiar line.
Now, Koch2 of 1
we go to the function definition, replace all the d's by 1.
First question asks "Is 1 equal to 0?" Of course not!
So we look at the local definition, d- is going to be 1 - 1 or 0
k is going to be a function that takes Koch2 of 0 and rotates it by some angle.
Koch2 of 0 is just that familiar line.
So we are going to have a list of 4 copies of that line
rotated 0, 60, -60, and 0 degrees.
And now we can take that list of images and paste them together using Apply and Beside/Align.
And we get that bumpy line.
Koch2 of 2, how do I understand that? Well, we go to the function definition
replace all of the d's by 2.
Is 2 equal to zero? Of course not!
The local definitions say that d- is equal to 2-1 or 1. K is a function that
takes Koch2 of 1 and rotates it through some angle.
Koch2 of 1 is that bumpy line we just produced, so the map expression
is going to produce a list of 4 copies of bumpy lines
rotated 0, 60, -60, and 0 degrees and now I can paste those 4 copies
together using Apply/Beside. That looks like that.
I bet you can guess what Koch2 of 3 looks like!
4 copies of Koch2 of 2 pasted together.
Even Koch2 of 4,
that needs a bit more room which I get with Control-D.. there you go!