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
- Why does vowel? only work within functions?
- It's a little bit complicated, but the short answer is it
was only implemented inside of functions.
- You're welcome to define it yourself:
(define (vowel? letter)
(member? letter '(a e i o u)))
- What's the best way to ask and question when I'm at home?
Review
- functions are the workhorses of scheme, it's important
to understand domain and range
- Smaller ideas: we learned about several data types: numbers,
words, sentences, booleans & functions
Composition of functions
Introduction
- Composition of functions: this is the idea that you can use
the result of one function as the input to another function.
- You can then "hook up" two functions this way to
invent a new, third function
- This is the heart of functional programming
Expressions
- The interaction between you and the Scheme interpreter
is called the read-eval-print loop (Bananas video)
- Everything you type into the interpreter is called an expression
- Atomic expressions (a single value, these are self-evaluating)
: 87
87
: 3.1415
3.1415
: -2.69
-2.69
: 2/3
2/3
: 4/2
2
- Compound expressions (made up of smaller expressions)
: (+ 3 4)
7
- What just happened?
- The leftmost element is the operator
- The other elements are the operands or arguments
- Call the function + on the arguments 3
and 4
- In general, they have the form (function arg1 arg2 ...)
- We think of this as (verb noun noun ...)
- "Do this (the verb, or function) to these folks (the
nouns, or arguments)"
- When the operator is on the left, it's called prefix
notation
- If someone were to write 3 + 4, that would be infix
notation - We don't do this
- If someone were to write 3 4 +, that would be postfix
notation (like HP calculators) - We don't do this
- Prefix notation is useful when calling procedures
that have and arbitrary number of elements
: (+ 1 2 3 4 5)
15
- What happens when you do this with non-associative operators?
(Answer: for the numerical built-in functions, it performs the
operation on the first two, then on that result and the next
argument, and so on)
- Semicolons ";" are comment delimeters, everything
from them to the right is ignored
: (- 4 2 2) ;; Same as (- (- 4 2) 2) ==> (- 2 2) ==> 0
0
: (/ 4 2 2) ;; Same as (/ (/ 4 2) 2) ==> (/ 2 2) ==> 1
1
- Do they always have to have arguments? What's the identity?
: (+)
0
: (*)
1
: (-)
*** ERROR -- Wrong number of arguments passed to procedure
- What are other numerical functions?
- + - * / (the standard 4)
- max - max of arguments (1 or more)
- min - min of arguments (1 or more)
- sqrt - square root of single argument
- abs - absolute value of single argument
- expt - num raised to power
- remainder - remainder when arg1 is divided
by arg2
- truncate - integer part of number (digits
left of decimal)
- We can combine them together, as in the following Nested
expressions, which can be arbitrarily complex
: (- (+ 5 8) (+ 2 4))
;; using result replacement
;; (- (+ 5 8) (+ 2 4))
;; (- 13 (+ 2 4))
;; (- 13 6 ))
;; 7
7
- This can be Pretty printed (by hitting the return
key on the mac)
: (- (+ 5 8)
(+ 2 4))
7
- Order of evaluation: Evaluate subexpressions first,
then apply function to arguments.
- Little people model
- Think of this as hiring other people to perform the evaluation
- Result replacement
- Try experimenting with the modeler
- Plumbing diagrams
- Pictorial way of looking at the evaluation of the expression
Let's define our own Functions
- Let's say I always need to multiply a number by itself:
: (* 5 5)
25
: (* 3 3)
9
: (* -1 -1)
1
- This can be generalized as follows:
: (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
- This is really powerful, let's study this for a while
- We have written the procedure as a generalization
of a technique
- What we've done is define a new function, just like +
- There's nothing special about x, it's just a variable,
or a formal parameter. We could have used bob
as our variable!
- (* x x) is the body of the expression
- Definition stops until overwritten or turned off
- define is a special form that doesn't follow
the standard evaluation model
- (+ 1 2) is the actual argument expression
- 3 is the actual argument value
- How would we define cube (using bob as
our parameter, just to show that there's nothing special about
x)? Later we'll see that bob doesn't tell us
much about the value it's supposed to take, so it's not so helpful.
: (define (cube bob)
(* bob bob bob))
cube
: (cube 2)
8
- How can we define cube in terms of square? (We'll
choose a new name so as not to confuse the original cube,
let's call it cube-using-square.) We can put dashes
- between names to concatenate them.
- We're going to use y as our parameter here, just
to show that even though cube had the free parameter
bob and square used x, we can use
anything we want.
: (define (cube-using-square y)
(* y (square y))
cube-using-square
: (cube-using-square 2)
8
- Composability
- Procedures are like building blocks we can use to create
other procedures
- How would we write sum-of-squares that implements
sum-of-squares(x,y) = x^2 + y^2 ?
==>
==>
Summary
- Today's big ideas: expressions and functions
- expressions are simple or compound (function argument1
argument2 ...), we learned how they are evaluated.
- functions: we showed you how to define your own functions,
what power you have!
- Smaller ideas: we learned about several data types: numbers,
words, sentences, booleans & functions
Next Time
- We'll look at words and true and false values some more...
Puzzle : What color is the bear?
- An explorer walks one mile due south, turns and walks one
mile due east, turns again and walks one mile due north. He finds
himself back where he started! He shoots a bear.
- What color is the bear? (i.e., where on the globe
must he be?)
- Think of three more spots on the globe where this would
work.
Game : Laser robots
- Two laser robots are placed on two opposite corners of a
rectangular city; the first - on the south-west corner, and the
second - on the north-east corner.
- The city street map is a grid of horizontal and vertical
roads.
- The robots move in alternating turns, where each move
is either horizontal (left / right) or vertical (up / down).
- The goal of each robot is to have its opponent enter its
line of fire (vertically or horizontally).
- What is the strategy for winning the game?
WWW Maven: Dan
Garcia (ddgarcia@cs.berkeley.edu)
Send me feedback