CSC 151.01, Class 18: Conditionals
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Questions
- Comparing the three kinds of conditionals
- Lab
News / Etc.
- Continue partners!
- Even though the first half of the GIMP reading is obvious, you should still read it.
- Exam 1 returned.
- I’m happy to talk to you about your work on the exam individually.
- I’m happy to answer general questions in our Q&A time.
- Please read the notes on the exam before meeting with me.
Rotating reminders
- Ask questions via email! I’m always happy to (try to) answer questions via email. If I take too long to answer, send another email (or even text, if it’s a reasonable hour).
Upcoming Work
- Assignment 4 due TONIGHT at 10:30 p.m.
- Lab writeup, problem ?, due Friday before class.
- Reading: The GNU Image Manipulation Program
- Reading: Programming the GIMP tools
Extra credit (Academic/Artistic)
- CS Table, TODAY at noon, 21 Feb 2017. Net neutrality. Reading packets should be available outside Curtsinger’s office or Orsera’s office (or at the back of our classroom)
- Women in CS, Tuesday, 21 February 2017: Applying for GHC Scholarships and more! (All are welcome to attend.)
- Thursday extras, Thursday, 23 Feb 2017, 4:15 p.m., Science 3821: The Future of MathLAN
- Met Opera Live in HD showing of Rusalka, Saturday, Feb. 25. Talk 11:30-noon, opera noon-3pmish.
- Prazak Quartet, Tuesday, 28 February 2017, 7:30 p.m., Herrick.
Extra credit (Peer)
- Ritalin Test squad, 2-4pm Saturday, Bucksbaum, The Wall
- NEW Jazz band concert Friday at 7:30 p.m. in Sebring-Lewis.
Extra credit (Misc)
- Host a prospective student. (March 5, April 8, April 23)
- NEW Fill out the Ologie survey at https://beehiveresearch.qualtrics.com/SE/?SID=SV_8qwPmzA0plsxCrb
Good things to do
- Diving championships this weekend
Some notes from yesterday
Please discuss with partner.
Can you solve exercise 3 without using lambda?
(define valid-component? (section <= 0 <> 255)) ; good
(define valid-component? ; worse
(lambda (val)
(<= 0 val 255)))
(define valid-component? ; even worse
(lambda (val)
(and (<= 0 val) (<= val 255))))
(define valid-component? ; much worse
(lambda (val)
(or (= 0 val)
(= 1 val)
(= 2 val)
(= 3 val)
...))) ; Ellipses are not really allowed in Scheme
If the average of the three components of c1 is less than the average of the three components of c2, what does that tell you about the relationship of the sum of the three components of c1 to the sum of the three components of c2. (Or vice versa?)
- Sometimes you save computation by thinking more carefully about the problem.
- It is not incorrect to divide both sides by 3, but it is inefficient.
Academic honesty on exam 1
Three of you did not sign the second statement. Take a few minutes to write down suggestions for what I should do. (You should have received an index card.) We will discuss your suggestions in the last few minutes of class.
Questions
Grading of the exam
- What does “times: 2” mean?
- You filled in the time log, so you get two bonus points.
- What does “errors: 5” mean?
- As a collective, you found 5+ errors, so you all get five points.
- What does “first: 2” mean?
- You get a reward for finishing the exam first.
- What does “Run: -3” mean?
- When I click the “Run” button, it doesn’t.
- What does “Epi. log: 1” mean?
- You wrote a log for the epilogue, and I appreciated it.
- You wrote “Don’t show the image” in multiple places in the exam. Why?
- If I tell you to create an image, I just want the image created. I
may never want to see it. There’s an
(image-save ...). - What is perfect formatting?
- Here are some things that make formatting better.
- Makes it easy to read/understand (or at least as easy to read as Scheme can be)
- Makes it easy to tell what the parameters to each procedure are.
(+ (- 23 5) (* 6 7 8)) : NOT GOOD - Indented according to the rules of Scheme indentation. Ctrl-i
- No spaces after open parens
- No spaces before close parens
(lambda (params)on a line by itself.- Each condition in
condon a line by itself.
Homework 4
- Can you help me translate what you wrote on the board yesterday to Scheme?
- See the whiteboard
- Can I take a picture of that incredibly helpful diagram?
- Yes
Five (?) kinds of conditionals
(or OPERATION1 OPERATION2)
(and OPERATION1 OPERATION2)
(if TEST
CONSEQUENT
ALTERNATE)
- Evaluates test. Test returns #t or #f. If test returns #t, evaluate and return the value of CONSEQUENT. If test returns #f, evaluate and return the value of ALTERNATE.
- Different than traditional Scheme evaluation. Why?
- We haven’t done true and false before.
- Usually, we do everything; here, we make a choice
(* 0 (really-long-expression) (another-long-expression))
- If the test returns neither true nor false …, the result is “trueish” and we evaluate the CONSEQUENT.
(when TEST
CONSEQUENT1
CONSEQUENT2
CONSEQUENT3)
- Runs the test. If it’s true (or trueish), does all of the consequents in sequence.
- If the test is false, doesn’t do anything else.
- When would you use When?a
- Often, we use it for checking preconditions. (when (not (<= 0 component 255)) (error “You idiot, that’s not a valid component!”))
- Note that if provides you with only one consequent, while when provides
you with multiple.
- Use when when you have multiple consequents
- Use when when you have no alternative
(cond
[TEST1 CONSEQUENT1a CONSEQUENT1b]
[TEST2 CONSEQUENT2]
...
[TESTn CONSEQUENTn]
[else ALTERNATE])
- Run each test in sequence. For the first test that holds (and only the first that holds), evaluate all of the consequents.
- Useful when you have more than one choice.
Lab
Writeup
Write up exercise 2 from the lab on conditionals.
Send your solution to csc151-01-grader@grinnell.edu.
Title your email CSC 151.01 Writeup for Class 18 (YOUR FULL NAMES).