# CS3L Once-a-Week (OaW) Lecture 2 (Order of Evaluation, Booleans, Conditionals) Domain & Range, Abstractionhttp://inst.eecs.berkeley.edu/~cs3/

## Review: Order of Evaluation and Advice from former students

• http://inst.eecs.berkeley.edu/~cs3/documents/scheme.eval.pdf

## Review: True and False

• Booleans are true and false values named after mathematician 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
A B (and A B) (or A B) (not B)
#f #f #f #f #t
#f #t #f #t #f
#t #f #f #t #t
#t #t #t #t #f

### 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 90)
'some-A
(if (> score 80)
'some-B
(if (> score 70)
'some-C
'D-or-F))))```
• 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 90) 'some-A)
((> score 80) 'some-B)
((> score 70) 'some-C)
(else 'D-or-F)))```

## Common Confusions

• What types are each of these?
• 'x
• '(x)
• (x)
• x
• With inch-count, the spec said it took a list of two numbers. We saw:
• (define (inch-count (feet inches) ...
• (define (inch-count '(feet inches) ...
• ...and within inch-count,
• (first '(feet inches))
• 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?
• (define (foo x) (x))
• What are good mnemonics for first and butfirst?
• What does (first (butfirst '(abc def ghi)) return and why?
• What does (butlast (butfirst '(abc def ghi)) return and why?

## Announcements

• 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...
• I (Dan) strive to be a perfect (7.0/7.0) teacher -- how can I improve? Tell me!
• You can go to ANYONE's lab (if there's room)
• 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 office hours: M10-11am in HFA C-30
• Cheating - serious, don't do it, we'll send folks to the Office of Student Conduct
• TAsiT = TAs-in-Training (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 2009-02-09 ... in 2 weeks!
• 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

## 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 +

## Abstraction

### General Idea

• 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?

### Functional Abstraction

• 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.

### Data Abstraction

• 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...

• Again, more economic bad news. IMHO, we're far from the nadir.
• Obama in his second week of office. The Republicans may not vote for stimulus package...
• The ramp-up to the super bowl ... go Steelers!