A Scheme program consists of a set of function definitions. There is no structure imposed on the program and there is no main function. Function definition may be nested. A Scheme program is executed by submitting an expression for evaluation. Functions and expressions are written in the form
(function_name arguments)
This syntax differs from the usual mathematical syntax in that the function name is moved inside the parentheses and the arguments are separated by spaces rather than commas. For example, the mathematical expression 3 + 4 * 5 is written in Scheme as
(+ 3 (* 4 5))
Lists are the basic structured data type in Scheme. Note that in the following examples the parameters are quoted. The quote prevents Scheme from evaluating the arguments. Here are examples of some of the built in list handling functions in Scheme.
(cons '1 '2) is (1 . 2) (cons '1 '(2 3 4)) is (1 2 3 4) (cons '(1 2 3) '(4 5 6)) is ((1 2 3) 4 5 6)
The first example is a dotted pair and the others are lists. \marginpar{expand} Either lists or dotted pairs can be used to implement records.
(car '(123 245 564 898)) is 123
(car '(first second third)) is first
(car '(this (is no) more difficult)) is this
(cdr '(7 6 5)) is (6 5)
(cdr '(it rains every day)) is (rains every day)
(cdr (cdr '(a b c d e f))) is (c d e f)
(car (cdr '(a b c d e f))) is b
(list 'a) is (a)
(list 'a 'b 'c 'd 'e 'f) is (a b c d e f)
(list '(a b c)) is ((a b c))
(list '(a b c) '(d e f) '(g h i)) is ((a b c)(d e f)(g h i))
(length '(1 3 5 9 11)) is 5
(reverse '(1 3 5 9 11)) is (11 9 5 3 1)
(append '(1 3 5) '(9 11)) is (1 3 5 9 11)
Definition expressions bind names and values and are of the form:
(define {\em id exp})
Here is an example of a definition.
(define pi 3.14)
This defines {\tt pi} to have the value 3.14. This is not an assignment statement since it cannot be used to rebind a name to a new value.
User defined functions are defined using lambda expressions. Lambda expressions are unnamed functions of the form:
(lambda ({\em id...}) {\em exp} )
The expression {\tt (id...)} is the list of formal parameters. and {\tt exp} represents the body of the lambda expression. Here are two examples the application of lambda expressions.
((lambda (x) (* x x)) 3) is 9
((lambda (x y) (+ x y)) 3 4) is 7
Here is a definition of a squaring function.
One difference between Guile and snd's interpreter is the fact that display is replace in snd by snd-print(define square (lambda (x) (* x x)))