ludus-scripts/recursion.ld

40 lines
1.2 KiB
Plaintext

& Lesson 11: `spingons` and recursion
& Here's our friend, the ngon
fn ngon! (n, size) -> repeat n { fd! (size); rt! (div (1, n)) }
& Here we're introducing one of the more important, and """difficult""" concepts in computer science, recursion
& Let's read this line-by-line
fn spin_gon! (n, size, angle, growth, times) -> {
if lte? (times, 0)
then :ok & this is the base case: what to do when we're done
& in this case, `:ok`--just a little signal all went well
else {
ngon! (n, size) & make an ngon
rt! (angle) & turn right
spin_gon! (n, mult (size, growth), angle, growth, sub (times, 1))
& ^^^^^^^^^ This is the recursive call
& `spin_gon!` calls itself!--make another spin_gon
& What does this change between invocations?
& * `mult (size, growth)`
& * `sub (times, 1)` (or we could use `dec (times)`)
& What would happen if we *didn't* make these changes?
}
}
& Play with `spin_gon!`: see what you can come up with.
& Also: see if you can't read what each call does.
& It's okay if you can't: 5 parameters is a lot.
& Clayson's examples:
& Example 1, p. 21
& spin_gon! (30, 2, 0.03, 1.02, 95)
& Example 2, p. 21
& repeat 3 {
& spin_gon! (4, 120, 0, 0.95, 50)
& rt! (0.25)
& }
& spin_gon! (4, 120, 0, 0.95, 19)