(define (count low high) (while (<= low high) (begin (print low) (set! low (+ low 1)) )))This is iteration, as we used to model flowers in previous examples.
Alternatively, we could use recursion to accomplish the same task. It looks almost the same, but with two major differences:
(define (count low high) (if (<= low high) (begin (print low) (count (add1 low) high) )))The "while" has changed to an "if", and the increment of "low" has changed to a call to the function itself.
One small change will add leaves to the ends of the branches. At the lowest level of recursion, when the "if" condition fails, we have it return a leaf for its "else" value:
The tree can be extended to 3D by rotating branches out of the plane. A more complex and realistic tree is created by using more than two branches at each branch point, and varying transformations stochastically.
Note that this same technique could be used to break up space in volume, to generate rooms in a building, for example.
(world (define level 6) (frac-tri #<1 0 1> #<-1 0 1> #<0 1 0> 1 level 5 2) (frac-tri #<-1 0 1> #<-1 0 -1> #<0 1 0> 1 level 5 2) (frac-tri #<-1 0 -1> #<1 0 -1> #<0 1 0> 1 level 5 2) (frac-tri #<1 0 -1> #<1 0 1> #<0 1 0> 1 level 5 2) )