Midterm Logistics Where? 4 Leconte (http://www.berkeley.edu/map/)http://www.berkeley.edu/map/ When? Monday, 4:10 to 5:40 What to do? –“Review problems”

Slides:



Advertisements
Similar presentations
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Slide 1. Slide 2 Administrivia Mid-semester survey this week (Thur/Fri) –you NEED to do this. Interviews should be starting in a few weeks. I apologize.
Six compound procedures and higher-order procedures.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
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.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Slide 1 CS3 Fall 2005 Lecture 9: Higher Order Functions.
Thirteen conditional expressions: letting programs make “decisions”
29-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
More on Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Introduction to Python
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Testing Michael Ernst CSE 140 University of Washington.
Testing CSE 140 University of Washington 1. Testing Programming to analyze data is powerful It’s useless if the results are not correct Correctness is.
Consider the statement 1000 x 100 = We can rewrite our original statement in power (index) format as 10 3 x 10 2 = 10 5 Remembering (hopefully!)
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
CPS120: Introduction to Computer Science Decision Making in Programs.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
CS101 Computer Programming I Chapter 4 Extra Examples.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
22/11/ Selection If selection construct.
1 CS161 Introduction to Computer Science Topic #9.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Testing CSE 160 University of Washington 1. Testing Programming to analyze data is powerful It’s useless (or worse!) if the results are not correct Correctness.
31/01/ Selection If selection construct.
CS 127 Exceptions and Decision Structures. Exception Handling This concept was created to allow a programmer to write code that catches and deals with.
Controlling Program Flow with Decision Structures.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Let’s try it one more time!. Allan Technique Programming Recursively 1.Decide that the problem needs a recursive solution. 2.Decide specifically what.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.4
CS314 – Section 5 Recitation 10
Class 11: Two-argument recursion
CS 326 Programming Languages, Concepts and Implementation
Testing UW CSE 160 Winter 2017.
IF statements.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Material in the textbook on pages
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Testing UW CSE 160 Spring 2018.
Conditionals & Boolean Expressions
Conditionals & Boolean Expressions
Conditions and Ifs BIS1523 – Lecture 8.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Testing UW CSE 160 Winter 2016.
The Metacircular Evaluator
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Recursion Taken from notes by Dr. Neil Moore
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
6.001 SICP Interpretation Parts of an interpreter
Lisp.
Presentation transcript:

Midterm Logistics Where? 4 Leconte ( 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 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!

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

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 else

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 ‘( ) –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

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