CSC 151.02, Class 21: Other forms of list recursion
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Friday PSA
- Questions
- Quiz
- Lab
- Debrief
Preliminaries
News / Etc.
- Hi, I’m SamR
- New partners!
- Don’t forget that we “Spring Forward” on Sunday morning. (And sorry, I know that means that you’ll be extra tired on Monday a.m.)
Upcoming work
- Flash Cards due Wednesday.
- Lab writeup for class 21: Exercise 5. Due before class Monday. (Or whenever TK has things do.)
- Reading for Monday
- Numeric Recursion
- Assignment 6 due Tuesday.
- Preliminary reports suggest that TK and I succeeded in making a shorter assignment.
- I’ve rearranged the problems. TK will do so soon, too. Check the problem numbers before submitting.
- Evidence suggests that problem 6 or 7 (permutations) is the most difficult.
Extra credit (Academic/Artistic)
- CS Extra Monday at 4:15 p.m. in 1023: “An Introduction to the Automatic Extraction of Keyphrases”. (Snacks at 4pm.)
- CS Table Tuesday at noon: Unknown topic.
- More info wherever TK keeps the info online.
Extra credit (Peer)
- More info wherever TK keeps the info online.
Extra credit (Misc)
- More info wherever TK keeps the info online.
Other good things
- GHS presents “The Little Mermaid” Friday, Saturday.
- Singers concert, Sunday, 4 p.m., Plymouth United Church of Christ, 4126 Ingersoll Ave., Des Moines.
- Men’s Tennis, Sunday at 9am and 2pm in the Field House.
- Host one or more prospective students.
Friday PSA
- Take care of yourselves.
- Moderation in all things, even CS homework.
- Consent is absolutely, positively, necessary.
Questions
Quiz
- Have fun!
- If you finish early, take a few minutes to sit quietly and medidate. You have too few options to do so in your lives.
What we hope you took from the reading
In many cases, you will find that when you write recursive procedures, you’re solving similar procedures to those you solved before.
For example, product looks a lot like sum.
To make yourself a more efficient programmer, identify patterns and keep them somewhere.
Example: Sometimes we want to look through a list of values and make sure that all of them meet some predicate.
I want to write all-number? in order to check the preconditions for sum
here’s a pattern for “everything in a list needs to meet the same predicate”
(define all-PRED?
(lambda (lst)
(or (null? lst)
(and (PRED? (car lst))
(all-PRED? (cdr lst))))))
To write all-number?, I just copy, paste, and change.
(define all-number?
(lambda (lst)
(or (null? lst)
(and (number? (car lst))
(all-number? (cdr lst))))))
Our goal for today is to get you thinking about some of the patterns you use (and, we hope, recording them somewhere).
You just said that copy-paste-change is a bad idea. Why?
- Some patterns will be wrong.
- But you may not realize that until you’ve copied and pasted and changed some number of times. You’ll have to go back and fix a lot of things.
- A better strategy is to have the computer do the work of copy/paste/change
- You’re going to learn how to do that after break.
- But you’ve already been doing it a bit.
Lab
Is it okay if smallest returns an inexact number when the smallest is exact, but the list contains a larger inexact number?
Yes.
What is our writeup?
Please writeup exercise 5.
Debrief
Determining if a list has exactly one element.
- Do not use
(= (length lst) 1)to determine if a list has one element. - Instead, use
(null? (cdr lst)). - Alternately, use
(and (not (null? lst)) (null? (cdr lst)))
Choosing return values
- Think about the type your procedure returns.
- The value you get in your base case should be the same type.
- E.g., since
smallestreturns a real number,nullis a bad thing to return in the base case.