Scheme Tutorial
(a rough guide for CS486 students)

Scheme at Waterloo

There are two useful Scheme systems at on undergrad.math. One is the Scheme->C system. This an implementation of the Scheme language by Digital Equipment Corporation. There are two programs sci and scc. sci is an interpreter, and scc generates compiled code (by translating the code to C). Warning scc can be very slow to run.

Another Scheme system available on some of the machines is MIT Scheme. This is the program scheme. This system has the advantage that it has a built in compiler, thus it can be a faster way to write your programs. Additionally, it can be installed on a PC. The home page for MIT Scheme is http://www-swiss.ai.mit.edu/scheme-home.html.

Note that evaluating the expression (exit) exits the Scheme system.

The Scheme Report (5) is the definitive reference for the Scheme language.

Introduction to Scheme

More about Scheme


EXERCISE 3.

Trees can be represented as recursive list structures. For example, the list
 ((1 2) 3 4)
can be viewed as representing the tree

              |
       --------------------
       |      |           |
     -----    3           4
     |   |
     1   2
In such structures we can walk the tree by doing recursion. We can detect when we reach a leaf node by using the predicate pair?

pair?returns #t if its argument is a cons cell (leaves are not pairs?). Note that in this structure only the leaves of the tree store useful information.

To Do:Write a recursive function called

 print-tree
Which takes a list and returns a string that contains the leaves of the tree from left to right, with no separators. See the description of
 (format #f ...)
and the description of (string-append ...) in the references contained in the Scheme resources page. Use the format specifier ~a to output the leaves.

Your function should handle null terminated branches properly, i.e., by outputting the empty string in their place.

Examples: your function should produce the following results

 (print-tree '())
 ==> ""

 (print-tree 'a)
 ==> "A"

 (print-tree '((1 2) (3 4)))
 ==> "1234"

 (print-tree '((1 2 ()) (3) 4))
 ==> "1234"