University of California, Berkeley
EECS Department - Computer Science Division

CS3 : Lecture 3 : Expressions & defining your own procedures


Overview of today's lecture


Answers to questions that have come up

(define (vowel? letter)
   (member? letter '(a e i o u)))

Review


Composition of functions

Introduction

Expressions

: 87
87

: 3.1415
3.1415

: -2.69
-2.69 

: 2/3
2/3

: 4/2
2
: (+ 3 4)
7
: (+ 1 2 3 4 5) 
15
: (- 4 2 2) ;; Same as (- (- 4 2) 2) ==> (- 2 2) ==> 0 
0

: (/ 4 2 2) ;; Same as (/ (/ 4 2) 2) ==> (/ 2 2) ==> 1
1
: (+) 
0

: (*) 
1

: (-)
 *** ERROR -- Wrong number of arguments passed to procedure
: (- (+ 5 8) (+ 2 4))
;; using result replacement
;; (- (+ 5 8) (+ 2 4))
;; (-    13   (+ 2 4))
;; (-    13       6 ))
;; 7
7
: (- (+ 5 8)
     (+ 2 4))
7

Let's define our own Functions

: (* 5 5)
25
: (* 3 3)
9
: (* -1 -1)
1
: (define (square x) 
    (* x x)) 
square

: (square 5) 
25

: (square (+ 1 2))
;; (square 3) [invocation]
;; (* x x) [body of square]
;; (* 3 3) [substition of argument values for formal parameters]
9
: (define (cube bob) 
    (* bob bob bob)) 
cube 

: (cube 2)
8
: (define (cube-using-square y) 
    (* y (square y)) 
cube-using-square 

: (cube-using-square 2) 
8
==>
==>

Summary

Next Time

Puzzle : What color is the bear?

Game : Laser robots


WWW Maven: Dan Garcia (ddgarcia@cs.berkeley.edu) Send me feedback

Dan Garcia Berkeley Computer Science