The Pan# User Manual
top | back | next

2   Getting Started

The best way to learn about Pan# is to write some programs. Here we present some basic examples for you to type in and run. You will need to use a text editor such as Textpad or Wordpad to enter these Pan# programs. Be sure to save programs as plain text in a file whose name that ends in .pan. A Pan# program is an unordered collection of definitions. The name picture is special, like main in Haskell. The value of picture is what a Pan# program displays. The definition of picture can refer to any other definitions in your program. Expressions in a Pan# program are always associated with a type that defines the set of values the expression may assume. Pan# types include numbers, points, colors, booleans, and images. There are really two languages in Pan#: one describing values and another describing types. Unlike some languages, you do not have to tell Pan what type a variable has -- a type inference engine in Pan can figure out the type of any expression or variable. However, it is good practice put type declarations in your programs to make them easier to understand. Pan# knows how to make a picture out of many different types. For example, this program displays a picture of a number:
picture = 2
To try this out, create a file named test.pan (or anything else ending in .pan) and enter picture = 2 in it. Save this file and double click it to see the picture. It is possible to use more complex mathematical expressions, as here:
picture = sqrt 2
Note that Pan (and Haskell) use sqrt 2 rather than sqrt(2). The sqrt function is one of many built-in math operators. The vocabulary of Pan# includes a large set of pre-defined functions, covered in Section 5. A control allows easy adjustment of the parameters which shape a picture. The slider control uses the mouse to adjust a numeric value within a defined range. Most controls have a GUI component which appears at the bottom of the picture or in a separate window, as controlled by the viewer. The following program takes the square root of a number between 1 and 10, as selected by a slider:
x <- slider "Select x" (1,10) 1
picture = sqrt x
The <- is quite different from the = used in the previous examples. The use of = indicates a definition: an expression which can be substituted in place of the name defined. The <- indicates a connection between the outside world (the slider control) and the definitions which form the picture. The arguments to the slider function are a label, in this case "Select x", that identifies the slider, the range of values, 1 to 10, and the initial setting, 1. A Pan program may contain function definitions. Try the following:
x <- slider "Select X: " (1,10) 1
f x = x*x - 1
picture = f x
The second line defines a function named f with an argument, x, where f(x)=x2+1. A picture is a function that maps an (x,y) coordinate onto a color. The default coordinate system in Pan is centered in the middle of the window and uses pixels as a unit of measure. The following program defines a parabola as a black shaded region:
picture (x,y) = if x > y*y/200 then black else white
Beside the normal rectangular coordinate system, Pan programs can also use polar coordinates. The notation (d @ a) refers to a point d units from the origin at an angle of a. This program defines a circle with an adjustable radius:
radius <- slider "radius" (0, 200) 50
picture (d @ a) = if d < radius then blue else yellow
Many more examples are packaged with the system. Section 4 contains examples that show off some of the more advanced capabilities of Pan#. Another good place to look is the Fun of Programming paper by Conal Elliott, available at www.conal.net. All of the examples in that paper are found in the fun directory of the demos.
The Pan# User Manual
top | back | next |
March, 2004