schemecop-40201 introduction to scheme. schemecop-40202 scheme meta-language for coding interpreters...

28
Scheme COP-4020 1 Introduction to Scheme

Upload: hannah-clarke

Post on 22-Dec-2015

228 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 1

Introduction to Scheme

Page 2: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 2

Scheme• Meta-language for coding interpreters

– “clean” semantics

• Scheme = LISP + ALGOL– simple uniform syntax; symbols and lists– block structure; static scoping– statement : evaluated for its effect– expression : evaluated for its value

• Dynamic type checking– flexible but inefficient (rapid prototyping)

Page 3: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 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 5ref

Page 4: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 4

Scheme Identifiers• E.g., y, x5, +, two+two, zero?,

etc• (Illegal) 5x, y)2, ab c, etc• Identifiers

– reserved keywords– variables

• pre-defined functions/constants• ordinary

• functions = procedures

Page 5: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 5

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)

Page 6: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 6

Special Forms

• Definition• (define <var> <expr>)

• Conditional• (if <test> <then> <else>)

(define false #f)(define false #f)

(if (zero? 5) 0 #t)(if (zero? 5) 0 #t)

Page 7: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 7

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->list, etc

Page 8: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 8

Symbols

• Identifiers treated as primitive values. Manipulated at run-time.– Distinct from identifiers that name variables in

the program text.– Distinct from strings (sequence of characters).

• Meta-programmingquote

symbol?

Page 9: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 9

Lists

• Ordered sequence of elements of arbitrary types (Heterogeneous)

• operations– car, cdr, cons, null?, ...– list, append, ...– first, second, ..., ninth

Page 10: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 10

Pairs

(cons ’a ’b)

(cons ’a (cons ’b nil) )

a b

a bnil ()

Page 11: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 11

Equivalence Test(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

Page 12: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 12

Vectors

• Both records and arrays provide random access to components. However, records are heterogeneous, while arrays are homogenoeus.

• Vectors are heterogeneous structures that provide random access to components using a computable index.

Page 13: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 13

• Constructors and accessors

(define v (vector 1 (+ 1 2)))#(1 3)

(vector-ref v 0)1

(vector-length v)2

• Index is 0-based.

Page 14: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 14

Procedures

• In Scheme, procedures are first-class objects. That is, they may be passed to or returned from procedures or stored in a data structure.

(if (procedure? 3) car cdr)#<procedure>

(procedure? append) #t

Page 15: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 15

( (if (procedure? procedure?) car cdr) (cons cdr car)) ’(list append))=( (car (cons cdr car)) ’(list append))=(cdr ’(list append))=(append)

Page 16: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 16

Apply-function(apply cons ’( x (y z)))

= (cons ’x ’(y z))

= (x y z)

(apply f ’(a1 a2 ... an))

= (f ’a1 ’a2 ... ’an)

(apply <func> <list-of-args>)

Page 17: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 17

(apply apply

(list procedure?

(list apply)))

= (apply apply

[ proc-fn [ apply-fn ] ]

)

= (apply proc-fn

[apply-fn] )

= #t

Page 18: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 18

Anonymous Fucntions

(lambda <formals> <body-expr>)

E.g.,

( (lambda (n) (+ n 2)) 5) = 7

• Evaluate actual argument expressions

• Bind these values to formals

• Evaluate body expression (static scoping)

Page 19: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 19

Variable Arity Procedures

(+ 1 2 3)

(append ’(1 (p q)) () ’(a b c))

(list 1 2 3 4 5)

(lambda <formal> <body>)• <formal> is bound to the list of actual

argument values supplied in a call.

Page 20: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 20

(define mul (lambda x (if (null? x) 1 (* (car x) (apply mul (cdr x)) ) )) ; 1 is identity w.r.t *) ; assuming * is binary(mul 1 (+ 2 3) 5)

Page 21: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 21

Binding constructs in Scheme

• define • binds value to a name.

• -function application• binds formal parameters to actual argument values.

• let-constructs • introduces local bindings

– let– let*– letrec

Page 22: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 22

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

Page 23: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 23

> (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)

Page 24: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 24

> (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*.

Page 25: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 25

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 )

Page 26: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 26

letrec-construct> (letrec ( (f (lambda(n) ((if (zero? n) 1 (f

(- 1 n)) ))) ) ) (f 5)

)> 1

> (letrec ( ( f (lambda () g) ) ( g 2 )

) ( f )

)> 2

Page 27: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 27

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.

Page 28: SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple

Scheme COP-4020 28

Branching constructs

(cond

(test1 exp1)

(test2 exp2)

(testn expn)

(else exp)

)

(case key

(keylist1 exp1)

(keylist2 exp2)

(keylistn expn)

(else exp)

)