In this chapter, we introduced recursion, a powerful programming technique that allows us to elegantly solve problems by taking advantage of “self-reference.” Fractals are graphically self-referential—that is, they contain smaller versions of themselves. We can implement recursion by simply letting a Python function call itself. Identification of the nonrecursive case, called the base case, is important to make sure that the recursive calls work correctly. Finally, we explored L-systems, which formally denote fractal behavior using a set of grammar rules. A string of symbols can be produced by repeatedly applying these rules. By interpreting this result string as a series of turtle commands, we can draw a fractal.
9.1 Modify the lSystem
function so that it recursively expands the rules. This will allow you to add a vertical bar ("|"
) operator that means “go forward,” but with the distance to go forward being scaled according to the depth of the recursion.
9.2 Research L-systems and find a different set of production rules to implement.
3.128.197.164