; 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:
(lambda formals body )You can think of this as evaluating to itself, with a rule for applying it to arguments (strip off lambda and substitute...)
(define variable expression)This special form evaluates the expression and
(define form body)is a variant of the define special form. Here form is in effect an expression of the form:
(variable arg1 ....argn)where variable is the name of the function being defined and
arg1 ... argnare formal parameters. For example:
(define (square x) (* x x))is a convenient way of defining the square function. However this kind of definition is only a convenience. So:
(define (variable arg1 ....argn) body)is exactly equivalent to
(define variable (lambda (arg1 ....argn) body)[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.]
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 not overlap - there may be others, e.g. files, and, in some environments, the widgets or windows which support graphical user interfaces. In this class we shall not be concerned with the types vector, char.
(if condition expr1 expr2)It evaluates condition. Unless the condition evaluates to #f then the result of the if expression is obtained by evaluating expr1 otherwise the result is obtained by evaluating expr2. [WHY does it have to be a special form? Could it NOT be a special form? ].
+ - * /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 (factorial 1000) , and (/(factorial 1000) (factorial 999))).
(quote expression)For example
(quote (+ 3 4))evaluates to the expression:
(+ 3 4)Quotation is very common, so we allow
'expressionas a shorthand. For example:
'(+ 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
(cdr '(+ 3 4))evaluates to
(car (cdr '(+ 2 3))evaluates to
2As well as quoting expressions that "make sense" in Scheme, such as '(+ 2 3) we can also use the quote notation just to make lists. For example
'(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 - car and cdr are funny names, derived from the assembly code of an antique IBM computer.]
null?recognises the empty list. It is an error to apply the functions car or cdr to the empty list.
(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 car is x and whose cdr is y . So to make a list containing just the number 23 we do:
(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.A symbol is recognised by symbol?.
Scheme has the convention that all letters occurring in a symbol are coerced into being lower case.
evaluates to the symbol:
This reflects the fact that Scheme converts all identifiers to lower case (as Pascal does, and C does not).