Check out the new USENIX Web site. next up previous
Next: Language Features Up: Nickle: Language Principles and Previous: Introduction

Programming With Nickle

A small example (Figure 1) may help give a feel for the Nickle language. Note that the variables i and t are declared at first use. The language feels much like C, but with some of the bothersome details of declaration and typing optional.

Figure 1: Function returning the number of ways that the first n elements of the array v can be added to produce c.
\begin{figure}\begin{verbatim}function countsum(c, v, n) {
if (c < 0)
retur...
...m(c, v, n - 1) +
countsum(c - v[n - 1], v, n - 1);
}\end{verbatim}\end{figure}

This much code would typically be placed in a file, and read into a running Nickle session (``> '' is the default Nickle prompt here and throughout)

  > load "countsum.5c"
During reading (which occurs with no perceptible delay) the input file is incrementally compiled into the running session. We can then interactively create a sample array to work with.
  > v = [5]{1, 2, 3, 4, 5}
  1 2 3 4 5
The square brackets are the array constructor, and in this case create a dynamically-typed array of 5 elements. The curly braces, as in C, are being used to surround an initializer list which values the array elements a[0] = 1 $\ldots$ a[4] = 5. Now we can invoke countsum() (which is extracted from a Cribbage scoring program).
  > countsum(15, v, dim(v))
  1
  > countsum(12, v, dim(v))
  2
If a new definition is given interactively for countsum(), it will scope out the current definition. As expected, the storage for v is allocated automatically, and released when v becomes unreachable.


next up previous
Next: Language Features Up: Nickle: Language Principles and Previous: Introduction
Bart Massey 2001-04-19