1.3. L system.

A Lindenmayer system, also known as an L-system, is a string rewriting system that can be used to generate fractals with dimension between 1 and 2. It is introduced by introduced by a Hungarian theoretical biologist and botanist, Aristid Lindenmayer, in 1968. It had then been driven lots of attention by scientists with various background, especially that of computer scientists, linguists and mathematicians, and the idea developed very quickly henceforth.

L system is defined as a tuple

G = (V, ω, P),
where

Below is an apple tree drew by a rule of an L-system. With proper coloring set of course (otherwise the outcome animation is merely combination of black bars).

L system
The Python code is described as follows

		import turtle

		def generateCommands(n, axiom = "F"):
			for x in range(n):
				axiom = axiom.replace("F", "FF+[+F-F-F]-[-F+F+F]")
			return axiom

		def generateTree(commands, len):
			stack = []
			for command in commands:
				if command == 'F':
					turtle.forward(len)
				elif command == '-':
					turtle.left(25)
				elif command == '+':
					turtle.right(25)
				elif command == '[':
					stack.append((turtle.position(), turtle.heading()))
				elif command == ']':
					previousPosition, previousDirection = stack.pop()
					turtle.penup()
					turtle.setposition(previousPosition)
					turtle.setheading(previousDirection)
					turtle.pendown()
			turtle.update()

		def prepareTurtle():
			turtle.tracer(0, 0)
			turtle.hideturtle()
			turtle.left(90)
			turtle.penup()
			turtle.goto(0, -turtle.window_height() / 2)
			turtle.pendown()

		prepareTurtle()
		fractalTree = generateCommands(5)
		generateTree(fractalTree, 7)
		turtle.exitonclick()