8]) (if (zero? n) (triangle 2 'solid 'red) (let ([t (sierpinski (- n 1))]) (freeze (above t (beside t t)))))) DrRacket editors can contain images, and DrRacket displays image values just like any other type of value (such as integers or lists). Running the above program, for example, actually displays a Sierpinski triangle, which can be cut and pasted into another program. The plot library constructs image values for more mature audiences and needs. For example, the following program plots the sum of two (three-dimensional) Gaussians, as concentric, partially transparent surfaces: #lang racket ;; Visualize a sum of two 3D Gaussians as concentric isosurfaces ;; Note: this example requires Racket 5.2 or later (require plot) ;; Returns an R x R x R -> R Gaussian function centered at (cx,cy,cz) (define ((gaussian cx cy cz) x y z) (exp (- (+ (sqr (- x cx)) (sqr (- y cy)) (sqr (- z cz)))))) ;; Lifts + to operate on three-argument functions (define ((f3+ g h) x y z) (+ (g x y z) (h x y z))) ;; Constructs an image value representing the sum of two Gaussians (plot3d (isosurfaces3d (f3+ (gaussian 0 0 0) (gaussian 1.5 -1.5 0)) -1 2.5 -2.5 1 -1 1 #:label "g")) ; labeling adds a legend Here, the isosurfaces3d function requires a three-argument function for its first argument, which the curried f3+ supplies. Besides constructing image values, plot can also write files in PNG, PDF, PostScript and SVG formats. GUI programming Racket implements a portable GUI layer which the libraries mentioned above build on. It is implemented via the native Windows API, via Cocoa on Mac OS X, and via GTK+ on Linux and others. The Racket API is a class-based toolkit, somewhat related to wxWidgets which was used originally. The following simple guessing game demonstrates coding with the GUI toolkit. The frame% class implements a top-level window, and button% implements a button. The check function defined here produces a function that is used for the button's callback action. #lang racket/gui ;; A GUI guessing game (define secret (random 5)) (define f (new frame% [label "Guessing game"])) ; toplevel window (define t (new message% [parent f] [label "Can you guess the number I'm thinking about?"])) (define p (new horizontal-pane% [parent f])) ; horizontal container (define ((make-check i) btn evt) (message-box "." (cond [(< i secret) "Too small"] [(> i secret) "Too big"] [else "Exactly!"]))