Lect 3.2 Scheme_nt - CPE425/CSC301/SC433 Programming...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
CPE425/CSC301/SC433 Programming Languages Lecture 3.2 Scheme
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Scheme 2 Topics Scheme Basic components Objects Binding forms: lambda, let, define, set! Data structures: lists, vectors Functions: primitive, user-defined Comparison of Scheme and FP Comparison of Functional and Imperative Languages
Background image of page 2
Scheme 3 Functions Any function application is written in prefix form: e.g. (+ 10 20) 10+20 (/ (* (+ 10 20) 5) 2) (10+20) * 5 / 2 (function_name arg1 arg2 arg3 …)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Scheme 4 Objects Scheme (like LISP) is a language for symbolic computation (AI applications) Values are represented by symbolic expressions (or S-expressions ) An expression is either: atoms e.g. a, 1, “hello world” lists e.g. (a b c d), (hello world) Vector e.g. #(a b c d)
Background image of page 4
Scheme 5 Binding Forms Syntactic forms used to bind or assign identifiers Lambda Let Definitions Assignments
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Scheme 6 λ - Calculus Developed by Alonzo Church Lambda-calculus is a formal system for functional definition in mathematical theory Anonymous functions A fundamental goal To described what can be computed LISP is based on λ-calculus
Background image of page 6
Scheme 7 f(x) = x + 3 (λx . x+3) λ - Function A mathematical function may be expressed in λ-form e.g. The lambda form clearly shows which variables are bound or free e.g. defining form bound variable f(x) = x + 3*a (λx . x+3*a) free variable bound variable
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Scheme 8 Lambda Expression in Scheme Aka lambda expression Analogous to λ -calculus expressions idspec : formal parameter s of procedure The expressions expr1, expr2 , are evaluated in sequence Creates (returns) an anonymous procedure (lambda (idspec) expr1 expr2 …)
Background image of page 8
Scheme 9 Lambda Expression in Scheme (cont’d) Examples (lambda (x) (+ x 3)) λx . x+3 ( ( lambda (x) (+ x 3) ) 7) λx . x+3 : 7 #<procedure> 10 ( ( lambda (f x) (f x x ) ) + 11) 22 ( ( lambda ( ) (+ 3 4) ) ) 7 + 11 11
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Scheme 10 Lambda Expression in Scheme (cont’d) Examples ((lambda (x) ((lambda (y) (- x y)) 15)) 20) λx . (λy . x-y : 15) : 20 The variable x is free in the body of the inner lambda expr, but its binding is found in the local environment for the outer lambda expr Global environment Local environment 1 x Local environment 2 y Result : 5
Background image of page 10
Scheme 11 Local Binding using Let Temporary (local) binding of identifiers to values in the body of let Order of evaluation of val_*, expr_* is at discretion of Scheme implementation Any free (i.e. unbound) variable appearing in val1 , val2 , … is looked up in a non-local environment (let ( (id1 val1) (id2 val2) … ) expr1 expr2 … )
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Scheme 12 Local Binding using Let (cont’d) Examples (let ( (a 2) (b 3) ) (+ a b) ) (let ( (sum (+ 2 4)) ) (* sum sum)) ( let ((b 3)) ( let ((b 10) (c b)) c ) ) 5 36 3 Global environment Local environment 1-a a, b Local environment 1-b sum Local environment 1-c b Local environment 2 b, c ( b = 3 (( b = 10) (c = b)) ) (1-a) (1-b) (1-c)
Background image of page 12
Scheme 13 Let and Lambda The let construct does not add any new semantic facility to Scheme Every let expression can be rewritten as a lambda expression applied to arguments (let ( (x 3) (y (+ 2 5)) ) (+ x y)) ( ( lambda (x y) (+ x y) ) 3 (+ 2 5)) 2 + 5
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Scheme 14 Using Let* Syntax is similar to let
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 57

Lect 3.2 Scheme_nt - CPE425/CSC301/SC433 Programming...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online