Scheme

An Introduction to Scheme
March 2008
1-1
Introduction
• A mid-1970s dialect of LISP, designed to be a
cleaner, more modern, and simpler version than the
contemporary dialects of LISP
• Uses only static scoping
• Functions are first-class entities
– They can be the values of expressions and elements of lists
– They can be assigned to variables and passed as parameters
1-2
Primitive Functions
1. Arithmetic: +, -, *, /, ABS, SQRT, REMAINDER,
MIN, MAX
e.g., (+ 5 2) yields 7
2. QUOTE -takes one parameter; returns the parameter without
evaluation
– QUOTE is required because the Scheme interpreter, named
EVAL, always evaluates parameters to function applications
before applying the function. QUOTE is used to avoid
parameter evaluation when it is not appropriate
– QUOTE can be abbreviated with the apostrophe prefix
operator
e.g., '(A B) is equivalent to (QUOTE (A B))
1-3
Primitive Functions
3.CAR takes a list parameter; returns the first element
of that list
e.g., (CAR '(A B C))
yields A
(CAR '((A B) C D)) yields (A B)
4.CDR takes a list parameter; returns the list after
removing its first element
e.g., (CDR '(A B C))
yields (B C)
(CDR '((A B) C D)) yields (C D)
1-4
Primitive Functions
5. CONS takes two parameters, the first of which can be
either an atom or a list and the second of which is a
list; returns a new list that includes the first
parameter as its first element and the second
parameter as the remainder of its result
e.g., (CONS 'A '(B C)) returns (A B C)
6. LIST - takes any number of parameters; returns a list
with the parameters as elements
1-5
Lambda Expressions
• Lambda Expressions
– Form is based on  notation
e.g., (LAMBDA
(L) (CAR (CAR L)))
L is called a bound variable
• Lambda expressions can be applied
e.g., ((LAMBDA
(L) (CAR (CAR L))) '((A B) C D))
Returns A
1-6
Scheme Functions
• A Function for Constructing Functions DEFINE Two forms:
1. To bind a symbol to an expression
e.g., (DEFINE
pi 3.141593)
(DEFINE two_pi (* 2 pi))
2. To bind names to lambda expressions
e.g.,
(DEFINE (cube x) (* x x x))
Example use: (cube
4)
Return 64
1-7
Scheme Functions (Cont’d)
• Evaluation process (for normal functions):
1. Parameters are evaluated, in no particular order
2. The values of the parameters are substituted into the
function body
3. The function body is evaluated
4. The value of the last expression in the body is the value of
the function
(Special forms use a different evaluation process)
1-8
Scheme Functions (Cont’d)
• Examples:
(DEFINE (square x) (* x x))
e.g., (square 4) yields 16
(DEFINE (hypotenuse side1 side1)
(SQRT (+ (square side1)
(square side2))))
e.g., (hypotenuse 4 3) yields 5
1-9
Scheme Functions (Cont’d)
• Predicate Functions: ( #T is true and () is false)
1. EQ? takes two symbolic parameters; it returns #T if
both parameters are atoms and the two are the
same
e.g., (EQ?
'A 'A) yields #T
(EQ? 'A '(A B)) yields ()
– Note that if EQ? is called with list parameters, the
result is not reliable
e.g., (EQ?
‘(A B) ‘(A B)) yields ()
1-10
Scheme Functions (Cont’d)
• Predicate Functions:
2. LIST? takes one parameter; it returns #T if the
parameter is a list; otherwise()
e.g., (LIST? ‘(A B)) yields #T
(LIST? 'A) yields ()
3. NULL? takes one parameter; it returns #T if the
parameter is the empty list; otherwise()
e.g.,
(NULL? ‘()) yields #T
(NULL? 'A) yields ()
(NULL? ‘(A B)) yields ()
1-11
Scheme Functions (Cont’d)
4. Numeric Predicate Functions
=, <>, >, <, >=, <=, EVEN?, ODD?, ZERO?,
NEGATIVE?
e.g.,
(= 3 3)
(= 3 7)
(EVEN? 4)
yields #T
yields ()
yields #T
• Output Utility Functions:
(DISPLAY expression)
(NEWLINE)
e.g., (DISPLAY “Hello World”)) yields Hello World
1-12
Scheme Functions (Cont’d)
• Control Flow
1. Selection- the special form, IF
(IF predicate then_exp else_exp)
e.g.,
(DEFINE (fact n)
(IF (=
n 0)
1
(* N (fact (- N 1)))))
e.g.,
(fact 5)
yields 120
1-13
Scheme Functions (Cont’d)
• Control Flow
2. Multiple Selection - the special form, COND
General form:
(COND
(predicate_1 expr {expr})
(predicate_2 expr {expr})
...
(predicate_n expr {expr})
(ELSE expr {expr})
)
Returns the value of the last expr in the first pair whose
predicate evaluates to true.
1-14
Scheme Functions (Cont’d)
(DEFINE (compare x y)
(COND
((> x y) (DISPLAY “x is greater than y”))
((< x y) (DISPLAY “y is greater than x”))
(ELSE (DISPLAY “x and y are equal”))
)
)
e.g.,
(compare 3 4) yields y is greater than x
1-15
Example Scheme Functions
1. member - takes an atom and a simple list; returns #T if
the atom is in the list; () otherwise
(DEFINE (member atm lis)
(COND
((NULL? lis) '())
((EQ? atm (CAR lis)) #T)
((ELSE (member atm (CDR lis)))
))
1-16
Example Scheme Functions
2. equalsimp - takes two simple lists as parameters; returns
#T if the two simple lists are equal;() otherwise
(DEFINE (equalsimp lis1 lis2)
(COND
((NULL? lis1) (NULL? lis2))
((NULL? lis2) '())
((EQ? (CAR lis1) (CAR lis2))
(equalsimp(CDR lis1)(CDR lis2)))
(ELSE '())
))
1-17
Example Scheme Functions
3. equal - takes two general lists as parameters; returns #T if the two lists are
equal; ()otherwise
(DEFINE (equal lis1 lis2)
(COND
((NOT (LIST? lis1))(EQ? lis1 lis2))
((NOT (LIST? lis2)) '())
((NULL? lis1) (NULL? lis2))
((NULL? lis2) '())
((equal (CAR lis1) (CAR lis2))
(equal (CDR lis1) (CDR lis2)))
(ELSE '())
))
1-18
Example Scheme Functions
4. append - takes two lists as parameters; returns the first
parameter list with the elements of the second parameter list
appended at the end
(DEFINE (append lis1 lis2)
(COND
((NULL? lis1) lis2)
(ELSE (CONS (CAR lis1)
(append (CDR lis1) lis2)))
))
1-19
Example Scheme Functions
• The LET function
– General form:
(LET (
(name_1 expression_1)
(name_2 expression_2)
...
(name_n expression_n))
expression {expression}
)
– Semantics: Evaluate all expressions, then bind the values
to the names; evaluate the body
1-20
Example Scheme Functions
(DEFINE (quadratic_roots a b c)
(LET (
(root_part_over_2a
(/ (SQRT (- (* b b) (* 4 a c))) (* 2 a)))
(minus_b_over_2a (/ (- 0 b) (* 2 a)))
(DISPLAY (+ minus_b_over_2a
(NEWLINE)
(DISPLAY (- minus_b_over_2a
root_part_over_2a))
root_part_over_2a))
))
1-21
Functional Forms
•
Functional Forms
1. Composition
• Only primitive functional form provided by the
original LISP.
• It is the essence of how EVAL works.
• All non-quoted lists are assumed to be function calls,
which requires their parameters to be evaluated first.
– Examples
(cdr (cdr ‘(a b c)))
(cons (car ‘(a b)) (cdr ‘(a b)))
1-22
Functional Forms
2. Apply to All
• One form in Scheme is mapcar
• Applies the given function to all elements of the given list; result is a
list of the results
– Example
(DEFINE (mapcar fun lis)
(COND
((NULL? lis) '())
(ELSE (CONS (fun (CAR lis))
(mapcar fun (CDR lis))))))
1-23
Functions that builds code
• It is possible in Scheme to define a function that builds Scheme
code and requests its interpretation using EVAL.
• This is possible because the interpreter is a user-available
function, EVAL
• e.g., suppose we have a list of numbers that must be added
together
(DEFINE (adder lis)
(COND
((NULL? lis) 0)
(ELSE (+ (CAR lis) (adder(CDR lis ))))
))
1-24
Functions that builds code
• An alternative solution is to write a function
that builds a call to + with propoer parameter
forms.
• This can be done with cons to insert the atom +
into the list. This new list can be submitted to
EVAL for evaluation.
((DEFINE (adder lis)
(COND
((NULL? lis) 0)
(ELSE (EVAL (CONS '+ lis)))
))
1-25
Scheme Features
• Scheme includes some imperative features:
1. SET! binds or rebinds a value to a name
2. SET-CAR! replaces the car of a list
3. SET-CDR! replaces the cdr part of a list
1-26