Title: Lets Scheme
1Lets Scheme
- A Quick Introduction to Programming in Scheme
2What Is Scheme?
- Statically scoped dialect of Lisp.
- Clear and simple semantics.
- First class procedures.
- Lambda expressions.
- Latent types (weakly or dynamically typed).
- Continuations have first class status.
- Prefix notation.
- Imperative, functional, and message passing
styles find convenient expression in scheme.
3Scheme Syntax
- ( 2 2)
- ( 2 3 4)
- ( ( 2 2) ( 3 3))
- (define x 2)
- X
4List Processing
- (quote (Pat Kim))
- (append '(Pat Kim) '(Robin Sandy))
- (length (append '(Pat Kim) (list
'(John Q Public) 'Sandy)))
5Variables
- (define p)
- (define x)
- (set! p '(John Q Public))
- (set! x 10)
- ( x (length p))
- (car p)
- (cdr p)
- (cons 'Mr p)
- (define town Halifax)
- (list p 'of town 'may 'have 'already 'won!)
6Functions
- (define (get-first-names name-list)
- (if (null? name-list)
- '()
- (cons
- (first-name (first name-list))
- (get-first-names (rest name-list)))))
7Functions (cont.)
hardcopy
- (define (first-name name)
- (first name))
- (define names)
- (set! names
- '( (John Q Public)
- (Malcolm X)
- (Admiral Grace Murray Hopper)
(Spot) (Aristotle) (A A Milne) - (Z Z Top)
- (Sir Larry Olivier)
- (Miss Scarlet)))
- (get-first-names names) gt ???
8Functions As First-class Objects
- (define (map-names fnc name-list) (if (null?
name-list) '() (cons (fnc (first
name-list)) (map-names fnc
(rest name-list))))) - (map-names first-name names) gt ???
9Variables and Values
- The value of a variable can be
- A number
- A symbol John
- A list (a b c)
- A function!
- Any of the above can be returned by a function.
10Apply
- (apply '(1 2 3 4))
- (apply append '((1 2 3) (a b c)))
- Apply expects the value of its first argument to
be a function.
11Map
- In general map takes an n-ary function and n
lists. It applies the function to the n first
elements of the lists, then to the n second
elements of the lists, etc. and returns a list of
the results. - (map '(1 2 3) '(1 2 3) '(1 2 3)) gt(3 6 9) (
treated as a ternary(n3) function here) - (map car '((a b) (d e) (g h))) gt (a d g) (car
is unary (n1))
12Example of map and apply
- "map-append" takes two arguments, a function fn,
and a list, and maps that function over the list
_appending_ together all of the results. (fn must
take one argument and return a list) - (define (map-append fn the-list) (apply
append (map fn the-list))) - (map-append cdr '((1 2 3) (1 2 3) (1 2 3))) gt (2
3 2 3 2 3)whereas - (map cdr '((1 2 3) (1 2 3) (1 2 3))) gt ((2 3) (2
3) (2 3))
13Unnamed Functions
- ((lambda (x) ( x 2)) 4) gt 6
- (map (lambda (x) ( x x)) '(1 2 3 4 5)) gt
(2 4 6 8 10) - lambda expressions make it possible to create new
functions at run time
14Binding constructs
- Three variants
- (let (bindings) form ...)
- (let (bindings) form ...)
- (letrec (bindings) form ...)
- bindings ((name init-form) ...)
- Evaluation
- binds each name to its init-form
- let bindings order unspecified
- let bindings done left to right
- letrec binds all names to undefined, evals each
init form, mutates bindings as needed - evaluates forms
15Example 1
(let ((x 2) (y 3)) (let ((x 7) (z ( x y)))
( z x))) gt 35 (let ((x 2) (y 3))
(let ((x 7) (z ( x y))) ( z x))) gt
70
16Example 2
(let ((x 5)) (letrec ((fact
(lambda (n) (if ( n 0)
1 ( n
(fact (- n 1))))))) (fact x))) What does
the evaluation of this form return? gt 120
17Equality
- (eqv? obj1 obj2)returns t if objects share
storage, else f. - (equal? obj1 obj2)returns t if objects display
the same. - (eq? obj1 obj2)like eqv? but stricter.
18More list operations
- (null? obj)returns t if obj is the empty list
- (list? obj)returns t if obj is a list
- null is a list
- if a is a list, then (cons x a) is also a list
- Membership tests
- (memq obj list) uses eq?
- (memv obj list) uses eqv?
- (member obj list) uses equal?
19Mutation of lists
- (set-car! list obj)mutates the car of list to
obj - (set-cdr! list obj)mutates the cdr of list to obj
20Evaluation model
hardcopy
- Every expression is either a list or an atom.
- Every list is evaluated to be either a special
form expression or a function application. - A special form expression is defined to be a list
whose first element is a special form operator.
The evaluation of the expression depends on the
particular special form operator. - A list is evaluated by first evaluating the
arguments and then applying the value of the
first element to the values of the rest of the
elements of the list. - A symbol evaluates to the most recent value that
has been assigned to that symbol (via "set!" for
example). - A nonsymbol atom evaluates to itself. These
include numbers and strings, and others.
21What is special about Scheme
hardcopy
- Built in support for lists.
- Automatic Storage Management.
- Dynamic typing.
- First-class Functions. Create new functions at
run time. Create unnamed functions. - Uniform Syntax. No complex precedence rules and
other structure. - Interactive Environment.
- Extensibility. Because of the uniform syntax and
other features it is easy to define your own
language on top of Scheme.
22How to Scheme
- To run the MIT Scheme interpreter on borg,
typeeem/software/scheme/bin/scheme - Put your Scheme function definitions in file.scm
in the current directory (if you use emacs, you
get some nifty features) - In Scheme, type (load file.scm)
- In Scheme, type Scheme expressions.
- Best to run Scheme from within emacs(ESC-x 2,
ESC-x shell splits your window and gets you a
shell within emacs in one window, in which you
can run Scheme.Edit file.scm in the other
window). - Consult the Scheme Reference Manual.
- Visit the Scheme links from the course web page.
23A (straight) list in Scheme
(define lst1 (a b c)) List lst1 is represented
as follows.
lst1
b
a
c
24A pair in Scheme
(define pair1 (a . b)) Pair1 is represented as
follows.
pair1
a
b
25Applying mutable operations to lists
implementation of queue
Starting with an empty queue, add a, b, and c to
the queue in this sequence, using the code in
question B1 of ass. 2. The resulting linked
structure, containing four cells, looks as
follows.
q1
b
a
c
Note that in Scheme set-car! and set-cdr! do not
introduce new cells, but just rewire their
arguments. Cons introduces a new cell.