Expressions in Lecture files can be evaluated | |
Read lecture notes on-line using UMASS Scheme as a browser Every Scheme expression occurring on a line by itself can be run. You can then try changing it and seeing what happens. Do not be afraid of playing with the machine - you cannot break it! 2 Forms of each file: HTML version plain-vanilla ASCII from menus in Scheme. You can execute the ASCII form I will modify UMASS Scheme to read HTML files |
Special Forms are not evaluated in the usual way. | |
The standard way to evaluate a Scheme expression is to evaluate the function and arguments and then to apply the function to the arguments. 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...) |
Definitions |
|
(define variable expression) (define form body) Here form is in effect an expression of the form: (variable arg1 ....argn) variable is the name of the function being defined arg1 ... argn are formal parameters. (define (square x) (* x x)) is a convenient way of defining the square function. (define (variable arg1 ....argn) body) exactly equivalent to (define variable (lambda (arg1 ....argn) body) |
Lambda Calculus, there is only ONE special form, namely lambda abstraction. Having a small number of special forms is mathematically desirable, The number of different cases to consider in mathematical analysis is reduced.] |
Boolean valued functions usually end in ? | |
Functions which return a boolean value (#t or #f ) are often called "predicates". By convention, predicates have a name which ends in a question mark. Certain common predicates like '<' are exceptions to this rule. |
Scheme Types. |
|
Objects which can exist in an implementation of Scheme belong to exactly one basic type. IEEE standard for Scheme requires at least boolean,char,null,number,pair, procedure,string,symbol,vector boolean? char? null? number? pair? procedure? string? symbol? vector? These predicates recognise object types which do not overlap Others: files, widgets or windows we shall not be concerned with the types vector, char. |
(if condition expr1 expr2) | |
The simplest form of conditional in Scheme is the special form: (if condition expr1 expr2) It evaluates condition. Unless the condition evaluates to #f the valueof the ifexpression 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? ]. |
Recursion - the factorial function | |
Defined using the recurrence relations: 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) 120 (factorial 1000) , (/(factorial 1000) (factorial 999))). |
Eager Evaluation | |
The rule that Scheme evaluates the arguments of an expression before applying the function is called eager evaluation, or call-by-value. Some functional languages, such as Haskell, are lazy, and evaluate no expression unless it is actually needed. They have a better logical structure (e.g. if does not need to be a special form in these languages), but are hard to make efficient. |