CSC 151.03, Class 16: Preconditions, revisited
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Friday PSA
- Questions
- Quiz
- Lab
News / Etc.
- New partners.
- Please make sure to return your computer cards to the jar.
- I was sorry to see so few of you at the Wilson Okello convo. It was one of the better convocations I’ve seen.
Upcoming Work
- Writeup for class 15 due TONIGHT at 10:30 p.m.
- Exercise 5
- To: csc151-03-grader@grinnell.edu
- Subject: CSC 151.03 Writeup 15 (YOUR NAMES)
- Writeup for class 16 due Monday at 10:30 p.m.
- Exercise 2
- To: csc151-03-grader@grinnell.edu
- Subject: CSC 151.03 Writeup 16 (YOUR NAMES)
- Assignment 5 due Tuesday at 10:30 p.m.
- Reading for Mondays’s class: Recursion basics
Extra credit (Academic/Artistic)
- Any of the many Grinnell Prize events.
- CS Table, Tuesday: Tapia.
Extra credit (Peer)
- ISO Fundraiser for Rohingya crisi in Myanmar, Friday, 5:30 p.m. JRC 209. (You need not donate to attend.) (Henna tatoos.)
Other good things
- Women’s Tennis vs. Cornell, Noon, Sunday (high-school courts)
- Volleyball vs. Knox Tuesday at 7:00 p.m.
- Volleyball vs. Beloit, next Friday at 7:00 p.m.
- Men’s Soccer Wednesday at 4:30 vs. Cornell
Friday PSA
- Think about your choices, in advance.
- Self gov (individually and collectively)
- Consent is absolutely positively necessary. But you should strive for more than consent.
Questions
- What is the difference between a Boolean and a predicate?
- Booleans are basic values, like integers, reals, and strings. The two Boolean values are
#tand#f. The design of Scheme is such that anything that’s not a Boolean is treated as “truish” and equivalent to#tin conditioanls. - Predicates are procedures that return Boolean values.
- We put question marks after both predicates and Boolean values to highlight that they are or return true/false.
- Can you help with problem 1 on the homework?
- Sure. Let’s look at what the problem says.
(define explore-lengths
(lambda (fname)
(let* ([words (file->words fname)]
[num-words (length words)]
; Given a (length frequency) pair, scale the
; frequency by the number of words.
[scale-frequency (lambda (oh)
(list "hi" (/ 2 num-words)))])
(map scale-frequency
(tally-all (map (section string-ref <> 0) words))))))
- I’m not sure what to put as the postconditions for
explore-lengths. - Yeah, that’s a hard one. You can’t be as precise, but say something about the number of bars you see and such.
- Kind of like the purpose, but perhaps more detail. Here, the purpose is “make a histogram of word frequencies” and the postcondition probably explains what each of the two potentially puzzling terms mean (“histogram” “frequency”)
- For problem 1, what should I do about word lengths that aren’t included? I feel like it distorts the results.
- Ideally, you’d come up with a clever solution. But it’s a rare enough case that I do not require you to come up with a solution.
- Suppose I have data for a stacked histogram. How do I figure out the
- number of entries in the legend?
- You have ‘((“a” (1 2 3)) (“b” ()) (“c” (6 1 3 2)) (“d” (5 1 4)))
- I can use
(map cadr data)to extract the lists - And so on and so forth
> (map cadr data) '((1 2 3) () (6 1 3 2) (5 1 4)) > (map length (map cadr data)) '(3 0 4 3) > (reduce max (map length (map cadr data))) 4 > (define len (reduce max (map length (map cadr data)))) - We’ll suppose that I have a predefined list of legends and colors.
I can use
(take legends len)and(take colors len) - Can you extend that example?
- Sure
> (define all-labels (list "twas" "brillig" "and" "the" "slithy" "toves" "did" "gyre")) > (define all-colors (list "red" "orange" "yellow" "green" "blue" "indigo" "violet" "black" "white" "gray" "purple" "pink")) > (define data '(("a" (1 20 8)) ("b" (10 10)) ("c" (3 4 5 6)) ("d" (2)))) > (define len (reduce max (map length (map cadr data)))) > (plot (stacked-histogram data #:colors (take colors len) #:labels (take legends len))) > (define data '(("a" (1 8)) ("x" ()) ("b" (5 5 5 5 5 5)) ("c" (3 4 5 6)) ("d" (2)))) > (define len (reduce max (map length (map cadr data)))) > (plot (stacked-histogram data #:colors (take colors len) #:labels (take legends len)))
Quiz
Lab
How do I define city?
:
(define city?
(lambda (val)
(and (list? val)
...
(string? (list-ref val 0))
(real? (list-ref val 1))
...)))
- Are there other inputs I should try?
(city? (list "50112" 52 -90))- Do I have to verify that the zip code is five digits?
- No.