W2511 Emergence & Lindenmayer Systems (Part 1)
- Read Emergence
- Read Lindenmayer System
- Read Terminal and Nonterminal Symbols
- View L-Systems - The Nature of Code
- Read Swift Sets
Begin a new project:
Create an empty project named Lindenmayer within your "project" directory.
cd ~/projects mkdir Lindenmayer cd Lindenmayer swift package init --type executable
Change to the source directory.
Edit the file main.swift.
Now, do the following. Be sure to think and plan BEFORE typing.
Design a ProductionRule class such that rules may be constructed using the following syntax:
let productionRules = [ProductionRule(predecessor:"1", successor:"11"), ProductionRule(predecessor:"0", successor:"10")]
Design an LSystem class such that an LSystem may be constructed using the following syntax:
let lSystem = LSystem(alphabet:["0", "1", "[", "]"], axiom:"0", productionRules:productionRules)
Implement a method for the LSystem class with the below signature which will return a set of all non-terminal (variable) characters in the alphabet. (In the above example, the non-terminals are "0" and "1".)
func nonTerminals() -> Set<Character>
Implement a method for the LSystem class with the below signature which will return a set of all terminal (constant) characters in the alphabet. (In the above example, the terminals are "[" and "]".)
func terminals() -> Set<Character>
Implement a method for the LSystem class with the below signature which will recursively produce the nth generation of the system. (In the above example the third generation would be "1111[11[10]10]11[10]10".)
func produce(generationCount:Int) -> String
As you're implementing these classes, be sure to validate input. At a minimum:
- Defining more than one production rule for the same predecessor is forbidden.
- Defining a rule for which the predecessor is not present in the alphabet is forbidden.
- Specifying an invalid axiom (i.e. an axiom for which any characters are not present in the alphabet) is forbidden.
If the input is invalid, the program should halt with a fatal error message.