The basic algorithm replaces a line segment
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcWKKkGzlqjocEf4Eh2SLDOj1XkE4h3uHgIZvg-nn41-mcNARveJPvsl43AEvGKP1thwldcRMWCO3VIgFhMZU4iigTVg6Q1grYaW-hmn0GV2FcLkxY8tWdpDFeGfIe1RlUS5ELCA/s320/iteration0.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1PBuS8wdinBVnOOYxExgByw_ea0LVCzyBoGHg0R7c-MyShgpfEJKrRQcCvHxgTXG5pWDtVxqm-cQokOYaClCr3mKwdkVwP_P-l5noQ4g81mocyGYfD6Dd4XcbxjeXAI2x8-dhZA/s320/iteration1.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixuxcZ_fWdiuTW8edcFBHskYB5Hbyfizx4HxMR_I6rMbYcUEKGXxLaoRwmv0w9-eOwtka-o7W_J_jBriAuBwNxB5eC9GeJL0yom3dMeBVmPy2onUBa9JHusvLA0mYZE2C4Ejl5tA/s320/fractal.png)
A direction is one of four symbols, 'N, 'E, 'S, or 'W. An orientation is one of two symbols, 'CW or 'CCW.
To rotate a direction 90 degrees clockwise or counter-clockwise, keep a "clock" of the cardinal directions, and rotate the clock index:
Then computing a fractal iteration is decomposed into two stages. The first stage computes the list of directions for each line segment in sequence. It recursively computes each iteration by replacing each direction in the previous iteration with a sequence of nine rotated directions.(define clock '(N E S W))
;; rotate : orientation * direction -> direction
(define (rotate or dir)
(let ([shift (if (eq? or 'CW) add1 sub1)])
(list-ref clock (modulo (shift (list-index (lambda (x)
(eq? x dir))
clock))
4))))
The second stage computes the actual line segments by simply "moving the cursor" from the starting point according to each subsequent direction.;; directions-for : nat * direction -> (listof direction)
(define (directions-for n dir)
(if (zero? n)
(list dir)
(append-map (lambda (d)
(list d
(rotate 'CCW d)
d
(rotate 'CW d)
d
(rotate 'CW d)
d
(rotate 'CCW d)
d))
(directions-for (sub1 n) dir))))
;; fractal-iteration : nat * (listof direction) * (cons nat nat)
;; -> (listof (cons (cons nat nat) (cons nat nat)))
(define (fractal-iteration len dirs point)
(let ([x (car point)]
[y (cdr point)])
(if (null? dirs)
null
(let ([point* (case (car dirs)
[(N) (cons x (- y len))]
[(E) (cons (+ x len) y)]
[(S) (cons x (+ y len))]
[(W) (cons (- x len) y)])])
(cons (cons point point*)
(fractal-iteration len (cdr dirs) point*))))))