day07

day07 - Caltech CS1 -- Fall 2008 CS1: Introduction to...

Info iconThis preview shows pages 1–15. 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

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: Caltech CS1 -- Fall 2008 CS1: Introduction to Computation Day 7: October 20, 2008 Higher Order Procedures pt. 2 (returning lambdas) Caltech CS1 -- Fall 2008 Today Some new Scheme constructs let , display , begin More on lambda procedures that return other procedures Caltech CS1 -- Fall 2008 let there be light Often want to define local values other than functions Could use internal define s Prefer to use new special form : let Caltech CS1 -- Fall 2008 Old usage Old way: (define (foo x y) (define z (+ (* x x) (* y y))) ; n.b. not a function (sqrt (* (- z x) (- z y)))) Or: (DrScheme Intermediate Student Language level) (define (foo x y) (local ((define z (+ (* x x) (* y y)))) (sqrt (* (- z x) (- z y))) ) ) Caltech CS1 -- Fall 2008 New Usage With let : (define (foo x y) (let (( z (+ (* x x) (* y y)))) (sqrt (* (- z x) (- z y))) ) ) Similar to local version, but dont need the define Caltech CS1 -- Fall 2008 Syntax & meaning ( let ((<var1> <expr1>) ; <var>: variable (<var2> <expr2>) ; <expr>: expression ) <body>) ;; can use var1 and var2 here let is like a "multiple define " can define multiple local names in a single let convenient, but exprs must not depend on previous vars ! Caltech CS1 -- Fall 2008 Syntax & meaning ( let ((<var1> <expr1>) ; <var>: variable (<var2> <expr2>) ; <expr>: expression ) <body>) ;; can use var1 and var2 here Can have one or more <var>/<expr> pairs <expr> is evaluated, bound to <var> Substitute value of <expr> for <var> in <body> Caltech CS1 -- Fall 2008 Hmm, that sounds familiar Substitute value of <expr> for var in <body> Where else have we seen this? lambda expressions Coincidence? I think not! Caltech CS1 -- Fall 2008 let is lambda in disguise ( let ((var1 <expr1>) (var2 <expr2>)) <body>) This is syntactic sugar for: (( lambda (var1 var2) <body>) <expr1> <expr2>) Substitution model is unchanged! let Caltech CS1 -- Fall 2008 let is lambda in disguise ( let (( x 1) ( y 2)) (+ x y) ) This is syntactic sugar for: (( lambda ( x y ) (+ x y) ) 1 2) Caltech CS1 -- Fall 2008 Why use let ? Can evaluate an expression once and use multiple times avoid unnecessary computations Can use it anywhere dont need internal define s for local variables no restrictions like with internal define s Can define multiple things in one let Caltech CS1 -- Fall 2008 let pitfall This doesn't work as you'd expect: (let ((x (* 2 3)) (y (* x 2))) (+ x y)) Might expect this to return 18 ( i.e. 6 + 12) Actually, might result in an error, or a different value, depending on value of x before the let expression Caltech CS1 -- Fall 2008 let pitfall (let ((x (* 2 3)) (y (* x 2))) ; not the x on prev line (+ x y)) Rule: the values of the exprs are evaluated using values that existed outside the let Desugaring let lambda will show why this is the case Caltech CS1 -- Fall 2008 let pitfall If you want to achieve the same effect, write nested let expressions: (let ((x (* 2 3)) (let ((y (* x 2))) (+ x y)))) Caltech CS1 -- Fall 2008...
View Full Document

This note was uploaded on 09/25/2010 for the course CS 1 taught by Professor Chang during the Spring '08 term at UMBC.

Page1 / 87

day07 - Caltech CS1 -- Fall 2008 CS1: Introduction to...

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