University of California, Berkeley
EECS Department - Computer Science Division
CS3 Once-a-Week (OaW) Lecture 3
(Order of Evaluation, Booleans, Conditionals) Domain & Range, Abstraction
Review: Order of Evaluation
- Have you read this document? (in the online reader files)
Review: True and False
- 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
- Anything not false (i.e., #f) is true!!!
- Example: = > < >= <= even? odd? equal? member? vowel?
- If you make your own predicates (returning only true & false values), end them with "?"
- A semi-predicate is something that returns false and something else (other than true), which can be useful.
- E.g., If I care about Cal's football team, but only want their margin of victory if they win, I could write a semi-predicate that returned #f if they lost but the margin-of-victory if they won.
- (cal-football-won-by 'washington-state) ==> 63
- (cal-football-won-by 'ny-giants) ==> #f ;; They'd prob lose against SB champs...
Combining predicates with and, not and or
- Usage: (and test1 test2 ... testN)
- Special form! Evaluates clauses left to right.
- If a clause evals to false, return #f.
- Otherwise (all clauses are true) return the result of the final clause.
- Think of and as a "false-sniffer", stopping when it finds one and returning it, otherwise it returns final clause.
- Usage: (or test1 test2 ... testN)
- Special form! Evaluates clauses left to right.
- If a clause evals to not-false, its value is returned.
- Otherwise (all clauses are false) return the result of the final clause (here, #f).
- Think of or as a "not-false sniffer", stopping when it finds one (and returning it), otherwise it returns final clause.
- Usage: (not test)
- Returns #t if test is false, and returns #f otherwise.
- Review of Boolean logic with examples
||(and A B)
||(or A B)
If : How we make decisions...
- Usage: (if condition action else-action)
- Special form! (the actions are only evaluated depending on the condition)
- I.e., only one of the actions is evaluated (thus making if a special form)
- If condition is not false, do action otherwise do else-action
- If the condition is false and there is no else-action, an #[undefined] value is returned.
- If is composable
- Even though it's a special form, if returns values just like another function, so it can be composed. E.g.,
(+ 365 (if (leap-year? year) 1 0)) ==> 366
cond: Writing conditionals
- What if you wanted to test among a series of things?
(define (get-cs3-grade score)
(if (> score 185)
(if (> score 165)
(if (> score 155)
- You can imagine if there were lots of different grades, this would get really long
- General form of cond
(cond (cond1 actions1 ... )
(cond2 actions2 ... )
(condN actionsN ... )
(else else-actions ... ))
- Cond evaluation rules:
- Evaluate conditions in sequence until one returns true value then evaluate its actions and return the last one
- else condition is fallout case - always true
- If all conditions are false (and there is no else clause), return value is #[undefined]
- Special form! (the actions are only evaluated depending on the condis)
- Watch out with parentheses
- Each cond/actions pair must be in parens.
- Conditions and actions that are function calls are in parens so you get the double paren as in (cond ((odd? ...
- Looking at the original grade program:
(define (get-cs3-grade score)
(cond ((> score 185) 'A+)
((> score 165) 'A )
((> score 155) 'A-)
- What types are each of these?
- What's wrong with
- (if (and (my-first-test foo) (my-second-test foo)) #t #f)
- (equal? bool #f)
- Why should we call other procedures if we can just do it ourselves?
- What does this do?
- What are good mnemonics for first and butfirst?
- The goal of lecture is to review Common Confusions, give demos, introduce high-level information, and share announcements (NOT work on exam-level questions)
- NOT to take dictation -- that's the purpose of handouts!! Eyes front, everyone...
- You can go to ANYONE's lab (if there's room)
- There's an anonymous feedback form on the inst cs3 website above (we'll post staff responses online too)
- We have 3 other websites
- ucwise : www.ucwise.org
- bspace : bspace.berkeley.edu
- WLA : wla.berkeley.edu
- The course recorded is an earlier CS3 with slightly different emphasis. Early lectures may work better than later ones (we deviate later). Very different lab experience (they don't learn as well as you do, more information has to happen in lecture). Same book, though!
- Dan's CS3L office hours: M2-3pm in 777 Soda (say how many attended)
- Cheating - serious, don't do it, we'll send folks to the Office of Student Conduct
- Giving away answers is as bad as receiving them
- TAsiT = TAs-in-Training (3 top students [our readers] who are pre-TAs)
- Homework 0 due in lecture next week
- No readers this year -- all files available online
- Quest (half-way between a quiz and a test) scheduled in-class for 2008-09-22
- 5% of total grade (basically, a sanity-check)
- Open-book, open notes (as are all our exams)
- Covers everything we've seen up to that date
- There will be a review session the weekend before
- Other admin questions?
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: word sqrt sentence +
- Arguably the most important part of Computer Science
- Definition: “The process of leaving out of consideration one or more properties of a complex object so at to attend to others”
- How many of you know how to drive a car? How many of those know how a car works?
- You don’t need to know how code works as long as it adheres to specifications (domain, range, side-effects)
- E.g.: double, whose body can be (* 2 x) or (+ x x))
- This is the fundamental principle that allows for outsourcing!
- E.g.: I give my detailed specs to some programmer in some other country where the cost of programmer labor is cheaper and they return me (hopefully nonbuggy) code.
- You don’t need to know how we represent data internally, only how to manipulate it.
- We can make up an Abstract Data Type (ADT), allowing us to create a new type of data (other than numbers, words, sentences, booleans and procedures, all you know about so far) and manipulate it.
Data Abstraction Example ... let's make a school!
- What pieces make up a school?
- Let’s make a first attempt to write the school data type...
(define (make-school name mascot)
(word name mascot))
- Anything wrong? Let's write it again, correctly.
- Now let's play with it and write has-same-mascot?
- Now let's modify make-school to have a location too
- Does has-same-mascot? work?
- Let's make sure we have software reuse!!
In Lab this week you'll see...
- Difference between Dates, a Case Study dealing with how many days are spanned between two dates
- This is a recurring problem: e.g., height differences, currency differences, etc.
- A Case Study...
- starts with a problem statement, ends with a solution (in between, a story, or narrative)
- documents "How a program comes to be"
- is written by a programming expert, you walk through it with the author and understand what the expert thinks about the process
- is used to teach you the process of coding -- this is called the Apprentice model of learning.
- can make you a better programmer (4 out of 5 educational researchers believe this!)
In Life this week you'll see...
- Our football team celebrating a drubbing of Washington St (66-3); next stop Maryland (n.b. Teams often have let-downs after a big wins [overconfidence?])
- Ensuing drama over the occupied tree near memorial stadium...or not?
- Let's Rock! Apple will release some new iPods on tuesday
@ 10am in SF!