University of California, Berkeley
EECS Department - Computer Science Division

CS3 OaW Lecture 4
DBD Case Study, Variables (globals, lets, shadows)

Review : Difference Between Dates (DBD) Case Study

Announcements

Common Confusions

(cond ((equal? day 31) (member? month '(1 3 5 7 8 10 12)))
      ((...

Variables

Global and Local Variables

: (define pi 3.14159)
pi

: pi 
==> 3.14159 

: x  ;; from the square program earlier 
==> *** ERROR -- Unbound variable: x
: (define (approx-pi) (/ 22 7))
approx-pi

: approx-pi 
==> #[procedure approx-pi] 

: (approx-pi) 
==> 3.14285714285714
==> (abs (- pi (approx-pi)))
0.00126714285714291
: (define (circle-perimeter diameter) (* pi diameter)) 
circle-perimeter 

: (circle-perimeter 2) 
==> 6.28318
: (define (triangle-area height width)  ;; 1/2 unknown
    (* 1/2 height width)) 
triangle-area 

> (triangle-area 2 3)
*** Error: unbound variable: 1/2

: (define 1/2 (/ 1 2))
: (triangle-area 2 3)
==> 3

Let : A way to do local variables

(let ( (variable1 value1)
       (variable2 value2)
       ...
       (variableN valueN) )
   body )
: (let ((a (+ 1 1))
        (b 3))
     (+ a b))
==> 5
: (let ((a (+ 1 1))
        (b a))
     (+ a b))
==> *** ERROR -- Unbound variable: a

Shadowing

Summary

In Lab this week you'll see...

In Life this week you'll see...