Lets Scheme - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Lets Scheme

Description:

(quote (Pat Kim)) (append '(Pat Kim) '(Robin Sandy)) (length (append '(Pat Kim) ... Note that in Scheme set-car! and set-cdr! do not introduce new ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 26
Provided by: evangelo
Category:
Tags: lets | scheme

less

Transcript and Presenter's Notes

Title: Lets Scheme


1
Lets Scheme
  • A Quick Introduction to Programming in Scheme

2
What 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.

3
Scheme Syntax
  • ( 2 2)
  • ( 2 3 4)
  • ( ( 2 2) ( 3 3))
  • (define x 2)
  • X

4
List Processing
  • (quote (Pat Kim))
  • (append '(Pat Kim) '(Robin Sandy))
  • (length (append '(Pat Kim) (list
    '(John Q Public) 'Sandy)))

5
Variables
  • (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!)

6
Functions
  • (define (get-first-names name-list)
  • (if (null? name-list)
  • '()
  • (cons
  • (first-name (first name-list))
  • (get-first-names (rest name-list)))))

7
Functions (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 ???

8
Functions 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 ???

9
Variables 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.

10
Apply
  • (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.

11
Map
  • 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))

12
Example 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))

13
Unnamed 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

14
Binding 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

15
Example 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
16
Example 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
17
Equality
  • (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.

18
More 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?

19
Mutation of lists
  • (set-car! list obj)mutates the car of list to
    obj
  • (set-cdr! list obj)mutates the cdr of list to obj

20
Evaluation 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.

21
What 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.

22
How 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.

23
A (straight) list in Scheme
(define lst1 (a b c)) List lst1 is represented
as follows.
lst1
b
a
c
24
A pair in Scheme
(define pair1 (a . b)) Pair1 is represented as
follows.
pair1
a
b
25
Applying 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.
Write a Comment
User Comments (0)
About PowerShow.com