## 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
• V (the alphabet) is a set of symbols containing both elements that can be replaced (variables) and those which cannot be replaced ("constants" or "terminals")
• ω (start, axiom or initiator) is a string of symbols from V defining the initial state of the system
• P is a set of production rules defining the way variables can be replaced with combinations of constants and other variables.

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).

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 == '[':
elif command == ']':
previousPosition, previousDirection = stack.pop()
turtle.penup()
turtle.setposition(previousPosition)
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()
``````