Lect 3.2 Scheme - Lecture 3.2 Scheme MAIN TOPICS • Scheme...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 3.2 Scheme MAIN TOPICS • Scheme • Comparison of Scheme and FP • Comparison of Functional and Imperative Languages References: – Louden, Ch. 11 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 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 …) 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) Scheme 5 Binding Forms • Syntactic forms used to bind or assign identifiers – Lambda – Let – Definitions – Assignments 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 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 Scheme 8 Lambda Expression in Scheme • Aka lambda expression • Analogous to λ-calculus expressions • idspec : formal parameters of procedure • The expressions expr1, expr2 , … are evaluated in sequence • Creates (returns) an anonymous procedure (lambda (idspec) expr1 expr2 …) 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 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 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 … ) 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 Scheme...
View Full Document

This note was uploaded on 12/08/2010 for the course SCE CSC301 taught by Professor Mr.leong during the Fall '10 term at Nanyang Technological University.

Page1 / 56

Lect 3.2 Scheme - Lecture 3.2 Scheme MAIN TOPICS • Scheme...

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

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