CSC 151.01, Class 19: Recursion basics lab
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Questions
- Quick notes on exam 1
- Quick review
- Lab
- Debrief
Preliminaries
News / Etc.
- New partners!
- You should have received exam 1 back last night. Sorry for the delay in grading. I’ll cover a few key points in class today.
Upcoming work
- Lab writeup for class 19: Exercise 6. Due before class Wednesday.
- Reading for Wednesday:
- Exam 2 due Tuesday
- Exam due Tuesday night
- Signed cover sheets in class on Wednesday.
- Epilogue due Wednesday night
- Flash Cards due Wednesday.
Extra credit (Academic/Artistic)
- Visit the two exhibits at the Faulconer Gallery.
- CS Extra Tuesday at 4:15 p.m. in 3821: “Computer Scientists Born of the Liberal Arts: What are Your Superpowers?”
Extra credit (Peer)
- Play this weekend. Tickets go on “sale” today. Thursday through Sunday. See the campus memo or elsewhere for details.
- Men’s Tennis, Sunday at 9am and 2pm in the Field House.
- Thursday, 7-9pm Gates, Escape Room.
Extra credit (Recurring peer)
- Listen to KDIC Wednesdays at 6pm - Witty banter with other personalities and/or co-host. Also Indian, Arabic, and Farsi music. (Up to two units of extra credit.)
- Peer editing with SS. Talk to SS about the details. Make your English Lit more literate.
Extra credit (Misc)
- Host one or more prospective students. And don’t just write “I hosted a prospie. I hope they go to Carleton”.
Other good things
- GHS presents “The Little Mermaid” Thursday, Friday, Saturday.
Questions
Non-exam Questions
I am unhappy with my grade on a homework. Can we talk about it?
Sure. I’ll review it. It will take a few days. Ping me if you have not heard back from me by Wednesday.
Can we switch the time of the mentor sessions?
Maybe. It depends on the mentors’ availability. But we’ll think about 9pm on Thursday.
And they will still email the quiz at 9pmish.
Exam Questions
Can we use recursion on the exam?
No.
Can you show us some sample output for 5b and 5c?
Sure.
How can I get “all the rest” to be black?
You have a list of speakers and a list of colors. Both are the same length.
I can figure out the “number” of a speaker in the list with
index-of.
If the name is not there, we get back -1.
You know about conditionals.
Will you comment on our prologues?
No, there isn’t time. It was more important to get the exams graded.
Do you really want nine graphs?
Yes.
Will you be upset if I submit twice?
Slightly, since it makes my work more difficult. But I’ll do my best to use the last submitted exam.
Do you want to see all of our notes?
Not really. But if your answer is wrong, it helps me give partial credit. And I’d rather you get as much credit as is reasonable.
On the evens-between problem, do you want us to require that the first parameter be exact?
Whatever you document is fine.
Do our extract-unique examples have to be as complex as yours?
No. Simple examples are fine.
Exam 1 quick notes
Format your code for clarity.
(lambda (params)on a line by themselves.- The first argument to a procedure call should be on the same line as the procedure. The remaining arguments will generally all be on the same line or all on separate lines.
- Limit lines to a width of about 75 characters.
- Use ctrl-i to indent correctly.
- Your overall goal is to make it easy for someone to trace the evaluation of your procedure.
Follow the standard format for the 6P’s.
- Each parameter should be of the form name, type.
- The types can have simple restrictions
- OK: “an exact integer less than ten”
- Not OK: “an exact integer less than ten which represents what might be my expected score on this problem, unless the other parameter is 5, in which case it represents Grinnell’s current US News ranking.”
- The product should be of the form name, type.
- The postconditions describe the product as carefully as you can.
Review
Count the stack of books
- If the stack has no books
- Say zero
- Otherwise
- Remove one book
- Ask someone else to count the remaining books
- Add 1 to whatever they gave you back
Find the books about writing
- If the stack has no books
- Give back no books
- Otherwise, if the top book is about writing
- Ask your assistant to find the remaining books about writing
- Add the top book
- Hand it back
- Otherwise
- Ask your assistant to find the remaining books about writing
- Hand them back
Key idea: You can solve a problem by solving a “simpler” version of the same problem. This idea is called “recursion”.
Note: Recursion always needs a point at which the input is simple enough to stop. That’s “the base case”.
Lab
Note on product:
> (sum null)
0
> (+)
0
> (product null)
?
> (*)
?
Note on list-length
- We wrote this algorithm in English on Friday.
Note on tally-numbers
- We wrote something like this algorithm in Engilsh in class on Friday.
- We just used “It’s a writing book” rather than “It’s a number”, but the concepts are the same.
Note on everything
- Avoid
whenfor most recursive procedures. You generally want to return something, and so should return something in every case.
Note on exercise 6
- Yes, you can use
maxormin. But you can’t usemaporreduce.
Note on exercise 7
- It’s spelled “kernel”
- Not “kernal”
- Not “colonel”
Writeup: Exercise 6
Debrief
We’ll probably do this next class, but here are some things to observe from yesterday’s lab.
It helps to think about how Scheme evaluates recursive procedures by repeatedly expanding the expression. (We’ve done that in the readings.) For example
(define product
(lambda (lst)
(if (null? lst)
1
(* (car lst) (product (cdr lst))))))
#|
(product '(5 3 4)) ; lst is not null
=> (* (car '(5 3 4)) (product (cdr '(5 3 4)))) ; simplify the car and cdr
=> (* 5 (product '(3 4)))) ; lst is not null
|#
You will often find that you start by writing `if` but end up writing
a `cond` expression.
(define tally-numbers (lambda (lst) (if (null? lst) 0 (if (number? (car lst)) (+ 1 (tally-numbers (cdr lst))) (tally-numbers (cdr lst))))))
vs.
(define tally-numbers (lambda (lst) (cond [(null? lst) 0] [(number? (car lst)) (+ 1 (tally-numbers (cdr lst)))] [else (tally-numbers (cdr lst))]))) ```
How to check if a list has one element:
- Not
(= 1 (length lst)). Why not? - But rather
(null? (cdr lst)). - Or perhaps
(and (not (null? lst)) (null? (cdr lst)))
Ways to write largest (stay tuned!)