Skip to main content

CSC 151.03, Class 11: Boolean values, predicate procedures, and list filters

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Questions
  • Notes from quiz 3
  • Lab
  • Debrief

News / Etc.

  • New partners.
  • Please rotate driver and navigator after each problem.
  • Please make sure to return your computer cards to the jar.
  • Thank you for all of the excellent exam questions. You will find many Q&A in the exam.
  • I did not have time to finish grading your quizzes this weekend. We will still go over them to help you understand what I expect for documentation and tests.

Upcoming Work

Extra credit (Academic/Artistic)

  • CS Table, Tuesday, noon, PDR A: Activism
  • CS Extras, Thursday, 4:15 p.m., 3821, Study Abroad in CS
  • One of the Happy Kareoke Fun Time activities
    • Musical, Friday 9pm, Gardner.
    • Workshop: Burn your fear. Saturday, 2pm. The Wall. Email [improv] to sign up.
    • Workshop: Creating through improv. Sunday, 2pm. The Wall. Email [improv] to sign up.
    • Musical, Sunday, 7pm, Gardner

Extra credit (Peer)

  • Women’s soccer Saturday.
  • Women’s soccer Sunday.

Extra Credit (Misc)

None right now.

Other Good Things

None right now.

Questions

Can I use filter to solve problem 5?
Not if you want credit for the problem.
Can I still gain extra credit for all my peers by identifying errors in
the exam?
Yes, until 10:30 p.m. Tuesday, when it is due.
Can they get extra credit for errors in the Q&A section?
No. Then folks would be getting more than 100 points of extra credit.

Notes from quiz 3

Part a: Documenting nearest multiple

;;; Procedure:
;;;   nearest-multiple   
;;; Parameters:
;;;   val, a real number
;;;   n, an integer
;;; Purpose:
;;;   Finds the nearest multiple of n to val.   
;;; Produces:
;;;   result, a real number
;;; Preconditions:
;;;   n > 0
;;; Postconditions:
;;;   * result is an integer multiple of n.  There exists an integer k
;;;     s.t. k*n = result.  (Alternately: n evenly divides result,
;;;     (remainder result n) = 0.
;;;   * result is the nearest multiple.  That is, for all other multiples
;;;     of n (say, m), we know that |val - result| <= |val - m|
  • Don’t rename parameters unless it makes them clearer. E.g., val1 and val2.
  • Don’t forget to give a name to the product
  • Don’t forget to give a type
  • Your reader may not understand “multiple”. For example, 7 is a multiple of 2. Becuase when I multiply 2 by 3.5 I get 7.
  • Parameters vs. Preconditions: Limitations on the input. Parameters the “easy” limitations (types). Preconditions are whatever is left (e.g., “not zero”; a > b, …)
  • Purpose vs. Postconditions: Both describe what the procedure does. Purpose informally; most people will understand. Postconditions: More formal, try to avoid terms that people may not share a definition of.
  • It would be fine to say n is an integer or real. However, you probably need a precondition that it’s not zero.
  • What should (nearest-multiple 10 4) return? (We are vague on this; that’s okay. It can return either 8 or 12.)

Part b: Testing

  • Particularly if you only have four tests, test different things.
  • Write tests that have a clear answer.
    • not (nearest-multiple 0 0).
    • not (nearest-multiple 10 4).

Lab and Debrief

How should I compare two strings for equality?
string=? and string-ci=? tend to be the two best options.
Do not use eq? or eqv?!
Do you care whether or not I use lambda when I can use compositions/section?
Nope. Some people prefer lambda. Some prefer section/compose.
You’ll generally find that the latter are shorter, but perhaps not much.
(define in-iowa?
  (lambda (row)
    (string-ci=? "IA" (list-ref row 4))))

(define in-iowa?
  (o (section string-ci=? <> "IA")
     (section list-ref <> 4)))
What is the difference between not and negate?
not takes a Boolean value as an input and returns the “opposite” Boolean value.
negate takes a predicate as an input and returns a new predicate that returns the opposite result
For example,
> (not #t)
#f
> (not #f)
#t
> (not 2)
#f
> (not real?)
#f
> (negate real?)
#<procedure:...ket/function.rkt:38:11>
> (define something? (negate real?))
> (something? 1)
#f
> (something? "")
#t
> (something? (list 1 2 3))
#t
> (something? something?)
#t
Anything else?
Remember the mantra: “Write something that works on individual elements or pairs of elements. Then apply it to the list with map (unary), reduce (binary), sort (binary comparator), or filter (unary predicate).”