Post

Simple L-Systems in Haskell

A basic text-based L-System in Haskell based on a Coding Train episode

Simple L-Systems in Haskell

Inspiration

This article is based on Daniel Shiffman’s coding challenge on YouTube: Coding Challenge #16: L-System Fractal Trees.

Simple L-Systems in Haskell

The following Haskell implementation is based on the original L-System example from the video linked above:

Axiom: “A”

Rules:

  • A -> AB
  • B -> A
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import Data.Maybe

type Rule = (Char, String)

-- Rules for the L-System
-- A -> AB
-- B -> A
myRules :: [Rule]
myRules =
  [ ('A', "AB"),
    ('B', "A")
  ]

-- Take a given character and set of rules, if the character is found in the rules
-- the corresponding replacement String is returned.
-- Otherwise return the original input character as a String.
applyRule :: [Rule] -> Char -> String
applyRule rules c = fromMaybe [c] $ lookup c rules

-- Apply the rules to each character in the original axiom
apply :: [Rule] -> String -> String
apply rules = concatMap (applyRule rules)

-- Show the first 5 iterations of running apply on the axiom "A"
main :: IO ()
main = print $ take 5 $ iterate (apply myRules) "A"
This post is licensed under CC BY 4.0 by the author.