Chapter 4. Tutorial I

Table of Contents
4.1. Lists
4.2. "Hello world" program

This tutorial contains examples of the basic types and mechanisms of Gamma programs. The first example shows you how to manipulate lists. A list is a very important data type in the Gamma language. The understanding of list manipulation is a key notion to many Gamma constructions. The second example walks you through the famous "Hello world" program.

4.1. Lists

The examples below demonstrate some of the basics of list manipulation. Since the Gamma language uses the Lisp engine, it inherits the rich set of list functions for which Lisp is known.

/* The program starts with the line containing the shell's
directive #!. It makes a Gamma program appear to be a
stand_alone executable to the user. The directive must appear
as the first line and must reference the actual Gamma
executable, not a link.  */

#!/usr/cogent/bin/gamma

/*
 * Lisp defines the functions 'car', 'cdr', and 'cons' as the basic
 * list manipulation functions.  The origins of these names have no
 * meaning on today's computers:
 *	CAR	- Contents of the Address Register
 *	CDR	- Contents of the Decrement Register
 *	CONS	- Construct (OK, this makes sense)
 *
 * We can define alternate names for car and cdr here:
 */

head := car;
tail := cdr;

/*
 * Create some lists.
 */

a = list ("My", "dog", "has", "fleas");
b = list (1, 2, 3, 4, 5, 6);

/* The pound sign "#" in front of a symbol prevents the Gamma
engine from evaluating the symbol so the symbol is taken 
literally. Thus, if x = 5, then the function call 
list (#x, 1) will create the list (x 1) rather than (5 1).
 */

c = list (#a, #b, #c, #d, #e, #f);	
d = list (#d, #h, #b, #i, #g, #e, #c);

/*
 * Take the first component of a list.
 */

e = head (a);
princ ("The first component of ", a, " is ", e, "\n");

/*
 * Take the tail of a list.
 */

e = tail (a);
princ ("\nThe tail of ", a, " is ", e, "\n");

/*
 * Concatenate two lists.  Notice that list elements do not need to
 * be the same type.
 */

e = append (a, b);
princ ("\nappending ", b, " to ", a, " gives:\n	", e, "\n");

/*
 * Walk a list and print each element
 */

princ ("\nThe elements of 'a' are:\n");
with i in a do
{
    princ ("	", i, "\n");
}

/*
 * Add an element to the beginning of a list
 */

princ ("\nAdding a zero to ", b, "\n");
b = cons (0, b);
princ ("    Gives: ", b, "\n");

/*
 * Take the first element of the tail of the list (the second element)
 * We can use combinations of car and cdr to do this.  However, the 
 * Gamma engine predefines a number of car and cdr combinations to 
 * make this easier, by inserting multiple 'a's and 'd's between the 
 * 'c' and the 'r' in the words car and cdr.
 * For example,   caddr(x)    is the same as    car(cdr(cdr(x)))
 *
 * The Gamma language defines all one-, two-, and three-letter 
 * combinations of car and cdr.
 */

princ ("\na is ", a, "\n");
x = car (cdr (a));
princ ("    car(cdr(a)) is: ", x, "\n");
x = cadr (a);
princ ("    cadr(a) is: ", x, "\n");

/*
 * Some other interesting list functions...
 */

princ ("\n");
princ ("Union of        ", c, " and ", d, " is ",
       union (c, d), "\n");
princ ("Intersection of ", c, " and ", d, " is ",
       intersection (c, d), "\n");
princ ("Difference of   ", c, " and ", d, " is ",
       difference (c, d), "\n");
princ ("Difference of   ", d, " and ", c, " is ",
       difference (d, c), "\n");