Warning This class is being recorded.
Approximate overview
Academic
Cultural
Peer
Wellness
Misc
Primary topic: Lists + “the big three” (filter, map, reduce)
I will also have another round of tracing.
Sample lists problem
Manipulate lists with fundamental higher-order list functions.
Write a procedure, (acronym string-of-words), that takes as input
a string of words separated by spaces and produces as output an
acronym that consists of the first letter of each word.
> (acronym "International Business Machines")
"IBM"
> (acronym "Grinnell's Underground Magazine")
"GUM"
> (acronym "Sam's Assorted Musings and Rants")
"SAMaR"
You may rely on this following helper function.
;;; (first-char str) -> character
;;; str: A non-empty string
;;; Extracts the first character of a string
(define first-char
(lambda (str)
(string-ref str 0)))
> (first-char "hello")
#\h
> (first-char "International")
#\I
TPS: What’s wrong with (or could be improved about) this solution?
You should be able to be able to find at least six different issues. (Invalid Scheme, won’t meet specifications, bad style, unnecessary code …)
(define grayscale
(lambda (c)
(cond [< 20 (simple-brightness c)
("black")]
((and (> (simple-brightness c) 20)
(< (simple-brightness c) 40)) ("dark-gray"))
((and (> (simple-brightness c) 40) (< (simple-brightness c) 60))
("medium-gray"))
((and (> (simple-brightness c) 60) (< (simple-brightness c) 80))
("light-gray"))
((> (simple-brightness c) 80)
("white")))))
“Wow, that’s really bad! Did that get an S?” (Yeah, probably.)
Do I have one hour total for the SoLA?
No. You have one hour for each LA you do.
But please stop at 15-20 minutes.
Do I have to do all the LAs at the same time?
No. You can take a break.
Can I move away from one LA and then come back to it?
Yes, provided you come back within the hour. The timer does not stop.
I am supposed to have extended time on tests and quizzes. Do I?
Try the sample LA.
Can I hack the system by submitting something and hoping the timer stops?
You can try, but the timer will keep going.
If we got an S on the Conditionals or Primitive types quizzes, can we skip the LAs?
Yes! Please skip those LAs.
What happens if you get it wrong the second time?
When will the real LAs show up?
10:00 a.m. today. (more or less)
Will collaboration be on the SoLA?
No
Will you ever grade the collaboration LA?
Yes. By week 14. Hopefully sooner.
When will we get mini-projects back?
Soon. Mini-project 2 will be immediately after class.
Can we talk about the relationship between cut and section?
Both are intended take a procedure and fill in some of the parameters, thereby creating a new procedure.
sectionis what we used in years passed (years past)
cutwas introduced this year to provide what might be a simler model.
(cut (+ <> 5))- Write an expression, put in diamonds for “I need a parameter”, add thecut.
(section + <> 5)- Doesn’t use the parentheses.
Sam wrote them both. But the design predates Sam’s implementation.
Why is it called cut?
Because you are effectively cutting holes in an expression.
What’s the difference between apply and reduce?
applygenerally needs procedures that can take lots of arguments.(apply + (list 1 2 3 4 5)).
applywon’t work with binary procedures.
(define silly (lambda (x y) (* 2 (+ x y))))You can’t say(apply silly (list 1 2 3 4 5))because that’s the same as(silly 1 2 3 4 5), which givessillythe wrong number of parameters.
reduceworks with binary procedures. But rarely predictably.
Will cut be on Friday’s quiz?
No. I’ll try to avoid it. Thanks for the suggestion.
It’s time to return to something we covered early in the semester.
Six key components:
TPS: What do we know about each of these in Scheme?
The weird part of recursion is that we are solving the “smaller” problem with exactly the same solution as the smaller problem; we have to assume we’ve written something we haven’t written yet.
The magic recursion fairy makes it work.
We’re going to rephrase recursion in terms of “delegation”. When given a large problem, an executive will normally delegate most of the problem to an assistant. We’ll assume that their assistant will do the same.
Algorithm for counting cards:
Algorithm for counting vowels
In Scheme,
(null? lst).(car lst).(cdr lst).vowel? (we’ll pretend)(define count
(lambda (lst)
(if (null? lst)
0
; count the remaining elements
(+ 1 (count (cdr lst))))))
(define count-vowels
(lambda (lst)
(if (null? lst)
0
(if (vowel? (car lst))
(+ 1 (count-vowels lst))
(+ 0 (count-vowels lst))))))
(define count-odds
(lambda (lst)
(if (null? lst)
0
(if (odd? (car lst))
(+ 1 (count-odds lst))
(+ 0 (count-odds lst))))))
```