Lect 3.2 Scheme_nt

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

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

CPE425/CSC301/SC433 Programming Languages Lecture 3.2 Scheme

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

View Full Document
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 …)

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

View Full Document
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

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

View Full Document
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

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

View Full Document
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 …)
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

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

View Full Document
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
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 … )

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

View Full Document
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)
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

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

View Full Document
Scheme 14 Using Let* Syntax is similar to let
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern