University of California, Berkeley
EECS Department - Computer Science Division
CS3 Lecture 2 : Functions
Overview of today's lecture
Review & Preview
- Basically, we're going to go through the excellent book chapter
by chapter
- Last time: Showing off Scheme (Chapter 1)
- Today: Functions (Chapter 2)
- Next time: Expressions and Defining our own procedures (Chapter
3 & 4)
- After that: Word and Sentences, True and False (Chapters
5 & 6)
Functions
Introduction
- We use functional programming for this course
- The core idea is the use of functions
- Remember high school algebra when we were given f(x)
= 2 x + 1 ?
- f is a function which takes x as input,
or as an argument, and returns another number
- We won't inititally use the normal full scheme interface
for now, we'll use functions
- It's possible that the functions we show accept more arguments
in full scheme than functions shows
Arithmetic
- Let's explore with some arithmetic functions:
- + - * / sqrt remainder abs truncate
- These all take numbers as input and return numbers as output.
- Many more listed in the inside back book cover, experiment!
Words
- A word included english words, numbers and any string
of numbers and digits and some punctuation
- Examples: cal 795 soda whassup yrul82-class
- Words are another data type
- Let's explore some non-numeric functions that deal with words
- word is the main constructor, it takes words and
smushes them together into a new bigger word
- Others: word first butfirst bf last butlast bl
- These all take words as input and return words as output
- What happens with butlast a ? Why? Is the return
value a valid function?
- There are others, however that don't necessarily return words:
count
- There are many others in the inside back book cover, experiment!
Domain and Range
- What happens when our function is +, our first argument
is 1 and our second argument is two?
- What is the domain of a function?
- The things a function accepts as an argument.
- What is the range of a function?
- All the possible return values of a function
- Examples: sin sqrt / + remainder
Sentences and Booleans
- Sentences are even another data type
- Sentences are a bunch of words enclosed in parenthesis, like
(cal is number one)
- Many of the functions that work with words work with sentences
- sentence is the main constructor, it takes words
and sentences
- first butfirst bf last butlast bl
- Booleans are true and false values named after George
Boole
- Sometimes we need to make decisions, and we need functions
to return true and false values
- Example: > < >= <= even? odd? equal? member?
vowel?
- Some work purely on boolean values: and or not
Functions
- Many fish eat plankton and plants; others eat fish themselves.
- We can treat functions as data!
- Example: number-of-arguments
- Example: every first (electrical engineering computer
science)
- Example: keep vowel? fascetious
Summary
- Today's big idea: 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
Next Time
- We'll look at expressions and learn how to define our own
functions...
Puzzle : Folding a dollar
- What is the fewest number of folds it takes to fold a dollar
bill into a triangle?
Game : 10 coins with a twist
- You and a friend have a stack of 10 coins.
- On each person's turn, they remove either 1 or 2 coins from
the stack.
- However, on your turn you can now ALSO place 1 or 2 coins
back on the stack.
- The person who removes the last coin wins.
- Should you go first or second, and what's your strategy?
WWW Maven: Dan
Garcia (ddgarcia@cs.berkeley.edu)
Send me
feedback