Examples

A functional programming language for the Arduino

GERALD

GERALD—the fractal drawing robot—is a great example of why the functional programming paradigm is relevant to microcontroller platforms. The algorithm for tracing fractals (such as the Koch snowflake shown here) is intuitively expressed as a recursive procedure. GERALD is driven by stepper motors, which can be controlled accurately in software by sending sequences of high and low voltage signals to the microcontroller's General Purpose Input/Output pins. The low-level code to generate such sequences (which is essentially imperative) is also easily expressed in Scheme code, as sequences of side-effects are easily expressed as lists of (impure) procedures. For the full code, see 'examples/GERALD.ms' in the source code repository.


  ;; Snippet from examples/GERALD.ms

  (define (segment level)
    (if (zero? level)
      (forward 3)
      (begin
        (segment (- level 1))
        (left 60)
        (segment (- level 1))
        (right 120)
        (segment (- level 1))
        (left 60)
        (segment (- level 1)))))

  (define (snowflake level)
    (segment level)
    (right 120)
    (segment level)
    (right 120)
    (segment level)
    (right 120))

  (snowflake 2)