MT1Review

advertisement
Midterm Logistics
• Where? 4 Leconte (http://www.berkeley.edu/map/)
• When? Monday, 4:10 to 5:40
• What to do?
– “Review problems” from Thursday/Friday in UCWise.
– Two practice exams and solutions are available from
the main CS3 page and through UCWise.
– More practice exams available at
http://hkn.eecs.berkeley.edu/cgi-bin/examquery.cgi?cs3. Be careful though! Some of the older
exams test on things we have not gone over in class.
– Open note & open book, but watch the time crunch!
Difference Between Dates
• Three versions you should know:
1. Buggy version 1 (general-day-span)
2. Correct version 2
3. Recursive version 3
• Know how each version works, run through various situations (same
month, consec month, non-consec month, error cases) in each
version and trace the program’s execution
– They all obviously have different methods and use different helper
procedures.
• Know how procedures interact with each other
– What does a certain procedure expect as input? What would happen if
you didn’t give it the expected input?
• Re-read the reader. Know what the programmer was thinking at
each step of the design process. It makes it a lot easier to figure out
how something works if you know why it works.
Case Study
• Possible types of questions to expect:
– Add on some extra functionality to the original dayspan problem
• Example: days-from-beginning-of-year, days-till-end-of-year,
number-of-odd-days
• What you should do: Use as many helper procedures as you
can to solve the problem. If you’re writing a lot of code, you’re
thinking too much about it. A lot of the functionality that you
may want to do may already exist in the day-span code.
Write code like you’re writing for someone who doesn’t
know the program at all.
• Example: Instead of using (first earlier-date) just use (monthname earlier-date), this is clearer to someone who doesn’t
know the code at all.
• Be careful! You can use helper procedures from version 1 or
2 of day-span (version 3 if it is allowed, but this is unlikely).
Case Study
• Possible types of questions to expect:
– Troubleshoot a bug in day-span
• You may be given a slight change in the day-span code to
make it give an incorrect output. You may have to explain
why it is now wrong and what the incorrect output is.
– ((equal? month ‘january) 1) turns into ((equal? month ‘january)
2)…what will happen?
• You may be told that you’re now getting incorrect outputs
after an unknown code change. You may have to write what
changed in the code to make the output wrong (similar to lab).
– Example: (day-span ‘(january 1) ‘(january 1)) is now giving me
2 instead of 1. What change could have happened to the code
for this to occur?
• By the way, these are very easy examples, expect something
harder.
Case Study
• Let’s look at an old midterm question
Case Study
• Fall 04 #2
• So we need to add error checking functionality.
Where would be the most logical place to do
this?
– We want to make sure we catch the errors before we
do any wrong operations, so we should check before
we call day span, right?
– (define (day-span-validated earlier-date later-date)
if/cond <error checking here>
else <regular execution>
Case Study
• Fall 04 #2
• Now we have to think about what errors we
actually need to handle.
• What does the example provide us with?
– Impossible date number (January 0, February 30)
– Impossible month name (flugalmonth)
– Non-number form for date (January Three)
Case Study
• Fall 04 #2
• How are we going to handle the error checking?
• Our approach: Three cases to consider = three
helper procedures:
– valid-month-name?
– numeric-date?
– valid-day-in-month?
• Make sure that you use as many helper
functions as you can when coding it in.
Case Study
• Case in point:
• Say we want to check if a given date is valid for
a 30-day month.
• Does this code work?
– ((and (member? (first date)
‘(september april june november))
(> (second date) 30)) #f)
– Yeah…but, we didn’t use helper functions.
– ((and (member? (month-name date)
‘(september april june november))
(> (date-in-month date) 30)) #f)
– Could we do better?
Case Study
• Case in point:
• Say we want to check if a given date is valid for
a 30-day month.
• Does this code work?
– ((and (member? (month-name date)
‘(september april june november))
(> (date-in-month date) 30)) #f)
– Could we do better?
– ((> (date-in-month date) (days-in-month (month-name
date))) #f)
Accessors
• You have used them, but what are they?
– Accessors are procedures to access specific
information from a larger body of information
– e.g. month-name accesses the name of the month
from a sentence with ‘(<month name> <date> <year>)
– e.g. in your quiz, (name <celebrity number>)
accessed the name of the celebrity from a database
of celebrity information “behind-the-scene”
– Accessors mean that you can change the data format
without changing everywhere you access the info
Recursion
• Remember the two parts:
– A base case.
• Needs to prevent errors. Can’t get bf of empty sent.
– At least one recursive call.
• Needs to take the same number of arguments.
• Arguments must be getting smaller. (or else what?)
Boolean and Predicates
• The simple definition of a predicate: A
function that returns either #t or #f.
• We have a lot of them in scheme, but
they’re all useful in certain situations.
Boolean and Predicates
• equal? vs. =
• They both take in two arguments and return #t if
they are equal, #f otherwise.
• Why do we need two?
– What happens if you do (= ‘a ‘a)?
• Error. = only works with numbers. equal? works with
everything (words, sentences, numbers)
– Why don’t we just use equal? for everything then?
• Code simplicity, it is easier for someone who doesn’t know
scheme to understand what = does.
Boolean and Predicates
• member?
• Takes two arguments, first is a character
or a word, the second is a word or a
sentence.
• We check if the character/word is a
member of the second argument.
Boolean and Predicates
• Try these out:
– (member? ‘a ‘abcd)
• #t
– (member? ‘a ‘(abcd))
• #f
– (member? ‘a ‘(a b c d))
• #t
– (member? ‘ab ‘abcd))
• ERROR, trying to check a word against a word…
• would (member? ‘ab ‘ab) work?
– (member? ‘ab ‘(abcd))
• #f
– (member? ‘ab ‘(ab cd))
• #t
Boolean and Predicates
• And then, the obvious ones.
• They take in one argument and check if that argument is
of the specified type.
–
–
–
–
–
sentence?
word?
number?
procedure?
string?
• What’s that?
– Example “abc”
– empty?
– even?
– odd?
Special Procedures
• if
– Know the basic format:
if (predicate, which will return #t or #f)
(what we do if predicate is #t)
(what we do if predicate is #f)
– What happens if we don’t have code to run for
when the predicate is false?
• If the predicate is true:
– We run the “true” code and have no problems.
• If the predicate is false:
– We return “okay”, which is usually meaningless
Special Procedures
• cond
– Know the format:
(cond
((pred1) (what we run if pred1 is true))
((pred2) (what we run if pred2 is true))
((pred3) (what we run if pred3 is true))
…
(else (what we run if no preds are true))
)
– How many predicates can we have?
• As many as we want (memory pending)
– What if don’t fulfill any preds but we don’t write an
else?
• We return ‘okay’
Special Forms
• and
– How many arguments can it take?
• As many as it wants.
– It evaluates arguments from left to right. It stops the moment it
finds a false result and returns #f. If it does not find a false result,
it will return a true value (but not necessarily #t).
– For example:
What will this return?
(and (= 1 1) (= 2 2) (= 2 3) (= 4 4) (= 5 5))
• #f, it will check (= 1 1) and (= 2 2) and see they are true and
continue. When it sees (= 2 3), it knows it is false and returns #f.
– Do we check (= 4 4) or (= 5 5)?
• No, since we stop evaluating the moment we get a false result.
– What will this return?
(and (= 1 1) (= 2 2) (= 3 3) 4)
• 4, because 4 is a true value in scheme (anything not #f is true)
Special Forms
• or
– How many arguments can it take?
• As many as it wants.
– It evaluates arguments from left to right. It stops the moment it finds a
true result and returns a true value (but not necessarily #t). If it does not
find a true result, it will return #f.
– For example:
What will this return?
(or (= 1 1) (= 2 2) (= 2 3) (= 4 4) (= 5 5))
• #t, it will check (= 1 1) and see it is true and return #t.
– Do we check (= 2 2) (= 3 3) (= 4 4) or (= 5 5)?
• No, we stop the moment we see a true value.
– What will this return?
(or (= 1 2) (= 34 1) #f)
• #f, we check everything, but nothing gives us a true value, so we are forced
to return #f
– What will this return?
(or (= 1 0) #f (= 2 3) 26)
• 26, because we return the first true value we see, which in this case is 26.
Special Forms
• Example question:
– Consider this:
(define (strange a b)
(if (> a 3) (+ a 1) (* 2 b)))
– What would happen if we tried to invoke the procedure with this
expression?
(strange (+ 5 6) (/ 7 0)
– Student A says “This will give an error message because you
can’t divide seven by zero.”
– Student B says “There is no error because the first argument a
has the value 11, which is greater than 3, so strange never uses
the value of the second argument b, so it doesn’t matter if it b is
an error.”
– Who is right?
Special Forms
• Example question:
– A is right; this expression results in an error.
– It's not relevant that IF is a special form. What matters, in this
problem, is that STRANGE is *not* a special form, so when you
invoke it, Scheme follows its usual rule: First it evaluates all the
subexpressions, then it invokes the procedure. So Scheme tries
to compute (/ 7 0) before it ever has a chance to notice that the
body of STRANGE includes a call to IF.
– B would be right if the question asked about a direct invocation
of IF, such as (if (> 11 3) (+ 11 1) (* 2 (/ 7 0))). But that's not how
Scheme works.
Special Forms
• not
– How many arguments can it take?
• Just one.
– It evaluates the given argument. If the argument is a
true value, not will return #f. If the argument is #f, not
will return #t.
– Pretty simple:
(not #t) = #f
(not #f) = #t
– (not 5)?
• #f
Writing Predicates
• Now we have all the elements we need for
actually making the computer think and
make decisions.
• Be prepared to code something using only
if and cond and then again using only and
and or.
Writing Predicates
• Example question:
• Write this without using if, cond, and, or
and not:
• (define (even-sum-a? x y)
(even? (+ x y)))
• The sum of two odd numbers or two even
numbers is even.
Writing Predicates
• Example question:
• Write this without using if, cond, or any
math functions (like +, -, /).
• (define (even-sum-b? x y)
(or (and (odd? x) (odd? y))
(and (even? x) (even? y))))
• Can you do it better?
(equal? (even? x) (even? y)))
Writing Predicates
• Example question:
• Write this without using and, or, not, cond,
or any math functions (that leaves if).
• (define (even-sum-c? x y)
(if (even? x)
(even? y)
(odd? y) ) )
Writing Predicates
• Example question:
• Write this without using and, or, not, if, or
any math functions (that leaves cond).
• (define (even-sum-d? x y)
(cond ((even? x) (even? y))
(else (odd? y)))
Quotes and Parens
• We use quotes to pass characters, words, or
sentences in a procedure: (explode ‘hello)
• We use parens when we want to call our
procedures: (word part1 part2 part3)
• And, we also use parens to help represent a
sentence...
– But, we also need a quote at the beginning.
– ‘(this is my sentence)
Quotes and Parens
• What will (square 5) return?
– 25
• What will ‘(square 5) return?
– (square 5)
• It seems simple now, but it can get easily complicated
when you’re doing this in procedures.
• Which code is correct?
– (month-name earlier-date)
– (month-name ‘earlier-date)
• The first one, since it actually passes us the variable represented by
earlier-date…the second one will pass the word “earlier-date” to
month-name, and obviously “earlier-date” is not the name of a
month.
Quickies
(word? 25)  #t
(first (bf ''#t))  (first (bf (quote #t)))
(first (first (bf ''#t)))  ERROR
(member? 'abc 'xabcyz)  ERROR
(and 1 (= (+ 3 2) 5) 5)  5
(or 4 (/ 5 0))  4
(se (bf (se (first (se '(abc) 'def '(ghi))) (word)
(word 'xy (word)))) (bl '(rsi)))  (“” xy)
Some More Quickies
(to try yourself)
•
•
•
•
•
•
•
•
•
•
•
•
(first (butfirst ‘(cs3) ) )
(or 4 (/ 4 0) ‘so-true ‘super-true)
(and + ‘+ 5 (= 3 4) )
(and < ‘false (or #t) )
(word)
(sentence)
(sentence “”)
(sentence ‘butfirst ‘of ‘abc ‘is (butfirst abc) ) )
(if (and) (or) (and) )
(count (day-span ‘(january 0) ‘(january 0) ) )
(+ 1 (first (quotient (word 3 4) 3) ) )
(starts-with-prefix? ‘(X I V) )
Download