Given a function such as f(x) = x^2 + c, you can iterate it as many times as you like by computing f(f(...f(f(x))...)). In general, inserting a real number into such an equation will result in the iteration growing towards infinity. If c=0 and x<1, though, it will shrink to zero. In fact, when c=0 and x=1, iterating the function does not change the value at all!
When you generalize this to allow for complex numbers, then the behavior becomes more interesting. A given value for c may have several attractors, such as zero and infinity in the example above. Sometimes iterating a certain value enters into an orbit – perhaps it rotates between a finite number of points. Usually nearby points have the same behavior. The boundaries between regions with different behavior compose the Julia set of the function.
Recall that complex numbers, written x + iy, can be thought of as coordinates on a two-dimensional plane, such as an image. Using this relationship, we can visualize the Julia set of a function by coloring pixels (which correspond to a sampled complex number) based on their behavior over iterations in the function.
It is difficult (though by no means impossible) to detect orbiting points, but it is quite simple to tell if a value is increasing without bound. A point ...
Lisp's functionality provides convenient aspects to visualize Julia sets in this way.
In order to display a Julia set, we first need a way to output an image. The file format ppm is simple to create. After a header which specifies a few parameters (including the dimensions of the image), you just have to write the rgb values in bytecode. We open a stream with with-open-file, which is described in Input&Output. Here we open it with a specified element-type of unsigned-byte 8 since we need to write bytecode.
Lisp has a write-byte function which writes the binary representation of an integer to the specified stream. To write characters we use the char-code function to convert characters into integers which can be written. Entire strings (such as the header) must be written out one character at a time. To do so we use a for-each loop, which is an example of an imperative structure which has worked its way into the Common Lisp language. The syntax is similar to that of any other language. Semantically, it just executes the body for each character in the string header.
To write out every pixel's rgb values we use a different loop structure. This one is like the conventional for loop that iterates over a specified range of integers. Lisp provides other looping options as well. The nested loops access every pixel in a two-dimensional list, which is a large list containing lists of rows of pixels (which are themselves lists of three elements). That is, colors = [ row1 row2 ... rowN ] where rowi = [pixeli1 pixeli2 ... pixeliN] and pixelij = [r g b].
; let us write binary data to a file
; direct stream to output
; overwrite existing file
; write PPM file header to the file
; "P6~%" ; P6 means colors are in byte-code (P3 for ASCII)
; "~a ~a ~%" cols rows ; ~A prints object without escape chars, such as quotes
; "a%" 255 ; max color (intensity)