Next: , Previous: User Guide, Up: Top



4 Scheme

4.1 Introduction

Scheme is a programming language invented by Jerald J. Sussman and Guy L. Steel Jr. in 1975. Scheme is based on another language - Lisp which dates back to the fifties. It is a high level language, which means it is biased towards human, rather than machine understanding. The fluxus scratchpad embeds a Scheme interpreter (it can run Scheme programs) and the fluxus modules extend the Scheme language with commands for 3D computer graphics.

This document gives a basic introduction to Scheme programming, and computer graphics techniques - no prior experience with programming languages is assumed. We'll start by going through some language basics, which are easiest done in the fluxus scratchpad using the console mode - launch fluxus and press ctrl 0 to switch to console mode.

4.2 Scheme as calculator

Languages like Scheme are composed of two things - operators (things which do things) and values which operators operate upon. Operators are always specified first in Scheme, so to add 1 and 2, we do the following:

     fluxus> (+ 1 2)
     3

This looks pretty odd to begin with, and takes some getting used to, but it means the language has less rules and makes things easier later on. It also has some other benefits, in that to add 3 numbers we can simply do:

     fluxus> (+ 1 2 3)
     6

It is common to "nest" the brackets inside one another, for example:

     fluxus> (+ 1 (* 2 3))
     7

4.3 Naming values

If we want to specify values and give them names we can use the Scheme command "define":

     fluxus> (define size 2)
     fluxus> size
     2
     fluxus> (* size 2)
     4

Naming is arguably the most important part of programming, and is the simplest form of what is termed "abstraction" - which means to separate the details (e.g. the value 2) from the meaning - size. This is not important as far as the machine is concerned, but it makes all the difference to you and other people reading code you have written. In this example, we only have to specify the value of size once, after that all uses of it in the code can refer to it by name - making the code much easier to understand and maintain.

4.4 Naming procedures

Naming values is very useful, but we can also name operations (or collections of them) to make the code simpler for us:

     fluxus> (define (square x) (* x x))
     fluxus> (square 10)
     100
     fluxus> (square 2)
     4

Look at this definition carefully, there are several things to take into account. Firstly we can describe the procedure definition in English as: To (define (square of x) (multiply x by itself)) The "x" is called an argument to the procedure, and like the size define above - it's name doesn't matter to the machine, so:

     fluxus> (define (square apple) (* apple apple))

Will perform exactly the same work. Again, it is important to name these arguments so they actually make some sort of sense, otherwise you end up very confused. Now we are abstracting operations (or behaviour), rather than values, and this can be seen as adding to the vocabulary of the Scheme language with our own words, so we now have a square procedure, we can use it to make other procedures:

     fluxus> (define (sum-of-squares x y)
     		(+ (square x) (square y)))
     fluxus> (sum-of-squares 10 2)
     104

The newline and whitespace tab after the define above is just a text formatting convention, and means that you can visually separate the description and it's argument from the internals (or body) of the procedure. Scheme doesn't care about whitespace in it's code, again it's all about making it readable to us.