CSC 151.01, Class 23: Recursion Basics
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Questions
- Background
- Key ideas in recursion
- Sample recursive procedures
- Expressing recursion in Scheme
News / Etc.
- New partners!
Quiz topics
- Anonymous procedures
- Local bindings (
let
let*
) image-compute
- Heterogeneous lists and
car
,cons
, andcdr
Rotating reminders
- Visit mentor sessions! We have mentor sessions on Thursday evenings from 7:00-8:00 p.m. and 8:00-9:00 p.m. in the CS Commons.
- Visit review sessions! I run review sessions on Thursdays at 9am in this room.
About the exam
We will quickly look through the exam.
- Download the code for the exam
- Look at the problems.
- Reflect on how you might solve the problem
- Make a list of similar problems from HW, labs, readings
- Jot down notes
- List procedures that might help
- Send Sam questions
- Fill out the prologue
- Try to get extra credit for everyone by taking advantage of Sam’s inability to use a spelling or grammar checker.
- Generate a random number.
- Send Sam more questions
- Each time you make progress, send yourself a copy of the exam because DrRacket will eventually eat it.
Upcoming Work
- Exam 2 prologue due Friday.
- Exam 2 due next Tuesday.
- No lab writeup!
- Revisit: Recursion basics
Extra credit (Academic/Artistic)
- CS Extras, Thursday at 4:15 pm, Sam’s research and PM’s research
Extra credit (Peer)
- P.U. ‘n club, Saturday, 4:00 p.m., Younker lounge
Extra credit (Misc)
Good things to do
- Bagel party in Math commons, Thursday, 7-9 pm. Lox status still unclear.
Questions
Background
We are looking at ways to solve problems that involve lists of values and identifying characteristics of these lists.
- How many elements are in the list
- Which letters
- “First” according to some order (e.g., alphabaetically first)
- Put them in order.
Sample recursive procedures
- To count the cards in a list
- If you have no cards in the list
- 0
- Otherwise
- Remove 1
- Ask your assistant to count the rest
- Add 1 to that number
- If you have no cards in the list
- To put the cards in order
- If you have no cards in the list
- Return the empty list
- Otherwise
- Identify the alphabetically last one
- Hand the rest to your assistant, and ask them to put the cards in order
- When they hand them back, put your card at the end
- If you have no cards in the list
- To find the alphabetically last card
- If you have only one card
- The one you have is alphabetically
- Otherwise
- Take one
- Hand the rest to your assistant and say “find the last card”
- Compare to the card you kept. Use the latter of the two
- If you have only one card
Key ideas in recursion
In general
- Ask if the problem is so simple that the answer is obvious
- If so, give the obvious answer
- If not,
- Make the problem slightly simpler
- Apply the same algorithm
- Compute an overall result from that simpler result
- Base-case test: Is it simple
- Base case: What to do when it’s simple
- Recursive case: What to do when it’s not simple
- Simplify
- Do it again
- Do a little more computation
Expressing recursion in Scheme
;;; Procedure:
;;; len
;;; Parameter:
;;; lst, a list
;;; Purpose:
;;; To determine how many elements are in lst
;;; Produces:
;;; length, a non-negative integer
;;; Preconditions:
;;; [No addition] (it's really a list, not one of those list-like things)
;;; Postconditions:
;;; You can successfully call `(list-ref lst i)` for any i between 0
;;; and length-1 inclusive
;;; If you call `(list-ref lst length)` you get an error.
(define len
(lambda (lst)
(if (null? lst)
0
; Ask your assistant to count the rest of the list, then add 1
(+ 1 (len (cdr lst))))))
It may be strange that a procedure is calling itself, but it makes some sense in English.
(define largest
(lambda (lst)
(if (only-one-element? lst)
lst
???)))
(define only-one-element-bad
(lambda (lst)
(= (length lst) 1)))
(define only-one-element
(lambda (lst)
(null? (cdr lst))))
Note: In the future, please do not use (if (= 0 (length lst))
to find
out if the list has only one element.