Comments follow a semicolon

Remember - all expressions in this file can be evaluated

Special Forms are not evaluated in the usual way.

(define

Boolean valued functions usually end in ?

Scheme Types.

The (if

Arithmetic in Scheme

The factorial function - an example of recursion

Scheme evaluates expressions in an Eager way.

Quoted Expressions are Compound Data.

The empty list

Making Lists.

Two Identities which hold for Scheme Lists

; This is a comment (+ 3 4) ; add 3 to 4

[Note - this file currently exists in two forms - the HTML version which is the one you are reading, and a (nearly) plain-vanilla ASCII form which is what you get when you use the menus in UMASS Scheme. The ASCII form is the one you should execute. The eventual plan is to modify UMASS Scheme to read HTML files as a (rather limited) Web Browser, so that only the one set of text for the course is needed]

This rule does not hold for certain *special forms*.
Examples of special
forms we have seen so far are:

```
(lambda (x) (* x x))
```

we can think of this as having a pattern described by:
(lambdaYou can think of this as evaluating to itself, with a rule forformals body)

(defineThis special form evaluates thevariable expression)

(defineis a variant of theform body)

(wherevariablearg...._{1}arg)_{n}

are formal parameters. For example:arg..._{1}arg_{n}

(define (square x) (* x x))is a convenient way of defining the

(define (is exactly equivalent tovariablearg...._{1}arg)_{n}body)

(define[Remark: in the Lambda Calculus, there is only ONE special form, namely lambda abstraction. Having a small number of special forms is mathematically desirable, since the number of different cases to consider in mathematical analysis is reduced.]variable(lambda (arg...._{1}arg)_{n}body)

boolean,char,null,number,pair, procedure,string,symbol,vectorIt may provide others. The type of a given object is recognised by one of the following built-in functions.

boolean? char? null? number? pair? procedure? string? symbol? vector?That is these predicates recognise object types which do

(ifIt evaluatesconditionexpr1expr2)

+ - * /There are also the usual algebraic and trancendental functions. They operate on integers, rationals, floating-point representation of reals, complex numbers. Try:

(sqrt -1)the answer is the complex number:

0.0+1.0iNumbers can also be compared. There is a predicate

zero?which recognises whether a number is zero. Comparisons between real numbers are denoted by the conventional symbols.

< > <= >=

0! = 1 n! = n(n-1)!These translate into scheme as follows:

(define (factorial n) (if (= n 0) 1 (* n (factorial (- n 1))))) (factorial 5) 120Try

(quoteFor exampleexpression)

(quote (+ 3 4))evaluates to the expression:

(+ 3 4)Quotation is very common, so we allow

'as a shorthand. For example:expression

'(+ 3 4)

[Note that this single quote is *not*
a special form, but a syntactic shorthand
for the corresponding expression using ` quote` ].

(car '(+ 3 4))evaluates to

+while

(cdr '(+ 3 4))evaluates to

(3 4)and

(car (cdr '(+ 2 3))evaluates to

2As well as quoting expressions that "make sense" in Scheme, such as

'(1 2 3 4)is a list of four natural numbers.

You can quote numbers and other constant objects, but, since they evaluate to themselves, there is no point.

Scheme implementations provide convenience functions for accessing lists. For example

(cadr x) = (car (cdr x)) (caddr x) = (car (cdr (cdr x)))[Note -

null?recognises the empty list. It is an error to apply the functions

(car '()) Error: PAIR NEEDED Culprits: (), In file: /users/users3/fac/pop/poplocal/local/Scheme/lecture3.scm Calling sequence: car This error report was prepared for Robin Popplestone by Jeremiah Jolt, your compile-time helper.

(cons x y)makes a list whose

(cons 23 '()) (23)Now let's define a function to make a "sandwich" list:

(define (sandwich x y) (cons x (cons y (cons x '()))))So:

```
(sandwich 1 2 )
```

evaluates to:
(1 2 1)

(car (cons x y)) = x (cdr (cons x y)) = y

An isolated Scheme identifier can be quoted, for example ` 'a`.
As such, it is called a *symbol*, and is a member of one of the
basic datatypes of Scheme.

Scheme has the convention that all letters occurring in a symbol are coerced into being lower case.

'CaT

evaluates to the symbol:

cat

This reflects the fact that Scheme converts all identifiers to lower case (as Pascal does, and C does not).