Presentation is loading. Please wait.

Presentation is loading. Please wait.

Midterm Logistics Where? 2050 VLSB When? Monday, 4:10 to 5:40 What to do? –“Review problems” from Thursday/Friday in UCWise. –Two practice exams and solutions.

Similar presentations


Presentation on theme: "Midterm Logistics Where? 2050 VLSB When? Monday, 4:10 to 5:40 What to do? –“Review problems” from Thursday/Friday in UCWise. –Two practice exams and solutions."— Presentation transcript:

1 Midterm Logistics Where? 2050 VLSB 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/exam- query.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!

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

3 Case Study Possible types of questions to expect: –Add on some extra functionality to the original day- span 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 (month- name 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).

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

5 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 ‘( ) –e.g. in your quiz, (name ) 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

6 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?)

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

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

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

10 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

11 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? –empty? –even? –odd?

12 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

13 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’

14 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)

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

16 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

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

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

19 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)))

20 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) ) )

21 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)))

22 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)

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

24 Quickies (word? 25)  #t (first (bf ''#t))  #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)

25 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 ppt "Midterm Logistics Where? 2050 VLSB When? Monday, 4:10 to 5:40 What to do? –“Review problems” from Thursday/Friday in UCWise. –Two practice exams and solutions."

Similar presentations


Ads by Google