Introduction to Scheme - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Scheme

Description:

Introduction to Scheme cs784(Prasad) L2Scm * – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 33
Provided by: TK172
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Scheme


1
Introduction to Scheme
2
Scheme
  • Meta-language for coding interpreters
  • clean semantics
  • Scheme LISP ALGOL
  • simple uniform syntax symbols and lists
  • block structure static scoping
  • expression evaluated for its value
  • statement evaluated for its effect
  • Dynamic type checking
  • flexible but inefficient (rapid prototyping)

3
Expressions
  • Literals Variables Procedure
    calls
  • Literals
  • numerals(2), strings(abc), boolean(t), etc.
  • Variables
  • Identifier represents a variable. Variable
    reference denotes the value of its binding.
  • x

5
ref
4
Expressible vs Denotable values
  • Booleans are expressible in (early) FORTRAN, but
    not denotable.
  • Functions are denotable in Pascal/C, but are not
    expressible.
  • In (functional subset of) Scheme, both value
    spaces are identical.
  • In (full) Scheme, variable references (pointers)
    are denotable but not expressible.

5
Scheme Identifiers
  • E.g., y,x5,,twotwo,zero?, etc
  • (Illegal) 5x,y)2,ab c, etc
  • Identifiers
  • reserved keywords
  • variables
  • pre-defined functions/constants
  • ordinary
  • functions procedures

6
Procedure Call (application)
  • (operator-expr operand-expr ...)
  • prefix expression (proc/op arg1 arg2 arg3 ...)
  • Order of evaluation of the sub-expressions is
    explicitly left unspecified by Scheme.
  • cf. C is silent about it.
  • cf. Java specifies a left to right processing.
  • ( x (p 2 3))
  • ((f 2 3) 5 6)

7
Special Forms
  • Definition
  • (define ltvargt ltexprgt)
  • gt (define false f)
  • Conditional
  • (if lttestgt ltthengt ltelsegt)
  • gt (if (zero? 5) 0 t)
  • gt (if '() 'emptyList 'never)
  • emptyList

8
Data Types
  • values, operations, canonical representation
  • Type-checking
  • static compile-time efficient
  • dynamic run-time flexible
  • numbers , -, , number?, , etc.
  • booleans t, f, boolean?, etc.
  • strings string?, string-gtlist, etc.

9
Symbols
  • Identifiers treated as primitive values.
  • Distinct from identifiers that name variables in
    the program text.
  • Distinct from strings (sequence of characters).
  • Primitive operations
  • quote
  • symbol?
  • Facilitates meta-programming

10
Lists
  • Ordered sequence of elements of arbitrary types
    (Heterogeneous)
  • operations
  • car, cdr, cons, null?, ...
  • list, append, ...
  • cadr, caadr, caddddr,
  • (cadar X) (car (cdr (car X)))

11
Pairs Expression, Internal Representation and
Print form
  • (cons 'a 'b)
  • (cons 'a (cons 'b '()) )

(a . b)
b
a
(a . (b . ()) ) (a b)
()
a
b
12
Domain Equations for defining S-Expressions and
Lists
  • Sexpr Atoms
  • U
  • Sexpr X Sexpr
  • List ()
  • U
  • Sexpr X List

13
Equivalence Syntactic vs Semantic
  • (eq? (cons 3 '()) (cons 3 '()))
  • f
  • (define a (cons 3 '()))
  • (define b (cons 3 '()))
  • (eq? a b)
  • f
  • (define c a)
  • (eq? a c)
  • t

14
Equivalence Syntactic vs Semantic
  • (equal? (cons 3 '()) (cons 3 '()))
  • t
  • (equal? (make-vector 5 'a)
  • (make-vector 5 'a))
  • t
  • (equal? (lambda(x)x) (lambda(y)y))
  • f
  • ? Formally unspecified

15
Vectors
  • Both records and arrays provide random access to
    components. However, records are heterogeneous,
    while arrays are homogeneous.
  • Vectors are heterogeneous structures that provide
    random access to components using a computable
    index.

16
  • Constructors and accessors
  • (define v (vector 1 ( 1 2)))
  • (1 3)
  • (vector-ref v 0)
  • 1
  • (vector-length v)
  • 2
  • Index is 0-based.

17
Procedures
  • In Scheme, procedures are first-class objects.
    That is, they may be (i) passed to procedures or
    (ii) returned from procedures or (iii) stored in
    a data structure.
  • (procedure? append)
  • t
  • (if (procedure? 3) car cdr)
  • ltproceduregt

18
  • (( (if (procedure? procedure?)
  • car cdr)
  • (cons cdr car) )
  • '(list append))
  • ( (car (cons cdr car))
  • '(list append))
  • (cdr '(list append))
  • (append)

19
Apply-function
  • (apply cons '( x (y z)))
  • (cons 'x '(y z))
  • (x y z)
  • (apply f '(a1 a2 ... an))
  • (f 'a1 'a2 ... 'an)
  • (apply ltfuncgt ltlist-of-argsgt)

20
Apply-function
  • Apply-function is not compelling if the function
    is of fixed arity and statically known.
  • Apply-function is indispensable if we wish to
    define variable arity function or the function
    will be computed dynamically.
  • Apply-function enables us to unify functions of
    different arities, and is an important component
    of an interpreter.

21
  • (apply apply
  • (list procedure?
  • (list apply)))
  • (apply apply
  • proc?-fn apply-fn
  • )
  • (apply proc-fn
  • apply-fn )
  • (procedure? apply)
  • t

22
Anonymous Functions
  • (lambda ltformals-listgt
  • ltbody-exprgt)
  • E.g.,
  • ((lambda (n) ( n 2)) ( 1 4) ) 7
  • Evaluate actual argument expressions
  • Bind these values to corresponding formals in
    formals-list
  • Evaluate body expression (static scoping)

23
Variable Arity Procedures
  • ( 1 2 3)
  • (append '(1 (p q)) '() '(a b))
  • (list 1.2 3/4 5)
  • (lambda ltformalgt ltbodygt)
  • ltformalgt is bound to the list of actual argument
    values supplied in a call.

24
  • (define mul
  • (lambda x
  • (if (null? x)
  • 1
  • ( (car x)
  • (apply mul
  • (cdr x))
  • )
  • )) 1 is identity w.r.t
  • ) assuming is binary
  • (mul 2 ( 2 2) 5)

25
Binding constructs in Scheme
  • define
  • binds value to a name.
  • l-function application
  • binds formal parameters to actual argument
    values.
  • let-constructs
  • introduces local bindings
  • let
  • let
  • letrec

26
let-construct
  • ( let
  • ( (var1 exp1) (varn expn))
  • exp
  • )
  • exp1 to expn are evaluated in the surrounding
    context.
  • var1,,varn are visible only in exp.
  • (let ( (x 2) (y 7) ) y)
  • 7

27
  • (let ( (x y) (y 7) ) y)
  • error y undefined
  • (define y 5)
  • (let ( (x y) (y 7) ) y)
  • 7
  • (let ( (x y) (y 7) ) x)
  • 5
  • (let ( (y 7) (x y) ) x)
  • 5 (not 7)

28
  • (define y 5)
  • (let ( (y 7) (x y) ) x)
  • 5
  • (let ( (y 7) )
  • (let ( (x y) ) x) )
  • 7
  • (let ( (y 7) (x y) ) x)
  • 7
  • let abbreviates nested-lets.
  • Recursive and mutually recursive functions cannot
    be defined using let and let.

29
letrec-construct
  • ( letrec
  • ( (var1 exp1) (varn expn))
  • exp
  • )
  • var1,,varn are visible in exp1 to expn in
    addition to exp.
  • (letrec ( (x (lambda() y))
  • (y (lambda() x)) )
  • x
  • )

30
letrec-construct
  • (letrec ( (f (lambda(n) (if (zero? n) 1 (f
    (- 1 n)) )) ) )
  • (f 5)
  • )
  • 1
  • (letrec ( ( f (lambda () g) )
  • ( g 2 )
    )
  • ( f )
  • )
  • 2

31
boolean connectives
  • (or test1 test2 testn)
  • (and test1 test2 testn)
  • or and and are not Scheme procedures.
  • They use short circuit evaluation rather than
    traditional call-by-value.

32
Branching constructs
  • (cond
  • (test1 exp1)
  • (test2 exp2)
  • (testn expn)
  • (else exp)
  • )
  • (case key
  • (keylist1 exp1)
  • (keylist2 exp2)
  • (keylistn expn)
  • (else exp)
  • )
Write a Comment
User Comments (0)
About PowerShow.com