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.

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
Lists Introduction to Computing Science and Programming I.
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.
Slide 1 CS3 Fall 2005 Lecture week 13: Introduction to the Big Project.
CS3: Introduction to Symbolic Programming Spring 2006Nate Titterton Lecture 4: "Difference Between Dates" and data abstraction.
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? 4 Leconte ( When? Monday, 4:10 to 5:40 What to do? –“Review problems”
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.
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.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Introduction to Python
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
Testing Michael Ernst CSE 140 University of Washington.
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.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
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.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
CS101 Computer Programming I Chapter 4 Extra Examples.
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.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
31/01/ Selection If selection construct.
CompSci 100E 18.1 Testing and Debugging Robert A Wagner.
CS 127 Exceptions and Decision Structures. Exception Handling This concept was created to allow a programmer to write code that catches and deals with.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
INF3110 Group 2 EXAM 2013 SOLUTIONS AND HINTS. But first, an example of compile-time and run-time type checking Imagine we have the following code. What.
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.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
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.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.4
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
A basic tutorial for fundamental concepts
Class 11: Two-argument recursion
Java 4/4/2017 Recursion.
CS 326 Programming Languages, Concepts and Implementation
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
Conditionals & Boolean Expressions
Last Class We Covered Data representation Binary numbers ASCII values
Conditionals & Boolean Expressions
Conditions and Ifs BIS1523 – Lecture 8.
The Metacircular Evaluator
Announcements Quiz 5 HW6 due October 23
Loops.
6.001 SICP Interpretation Parts of an interpreter
How to allow the program to know when to stop a loop.
Lecture 6 - Recursion.
Presentation transcript:

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

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? –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 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))  #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) )