CSC 151.01, Class 20: Other forms of list recursion
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Questions
- Problem 6 from Friday’s lab
- DrRacket tips and tricks
- Reading summary
- Lab
- Debrief (?)
News / Etc.
- I have not received all grades from the graders. I have not finished my own grading for CSC 151. I brought chocolate instead. I should have some grades on Wednesday.
- “Is it normal to need to ask questions on the exam?” Yes.
- “I feel like I’m asking a lot.” Some folks ask a lot. Others don’t. Don’t stress.
- “Can you talk about cool DrRacket tricks?” I’m not sure that any of them are cool, but I’ll go over the ones that come to mind.
- Do you know this student who is trash-talking CS?
- http://pioneers.grinnell.edu/news/2017/10/8/pioneer-womens-tennis-team-completes-perfect-run-through-mwc.aspx
- http://pioneers.grinnell.edu/news/2017/10/3/womens-volleyball-pioneers-roll-over-knox-for-first-mwc-victory.aspx
Upcoming Work
- No writeup for class 19.
- Writeup for class 20 due Wednesday at 10:30 p.m.
- Exercises 3d and 3e. No documentation necessary.
- To: csc151-01-grader@grinnell.edu
- Subject: CSC 151.01 Writeup 20 (YOUR NAMES)
- Reading for Wednesday’s class: Numeric recursion
- Exam 2 due Tuesday at 10:30 p.m..
- Exam 2 cover sheets due Wednesday at the start of class
- Exam 2 epilogue due Wednesday at 10:30 p.m.
Extra credit (Academic/Artistic)
- Aspen Trio on Wednesday at 7:30 p.m.
Extra credit (Peer)
- Volleyball vs. U. Dubuque, Tuesday at 7:00 p.m. (last home match!)
Extra credit (Misc)
Other good things
Questions
Problem 6 from Friday’s lab
(define furthest-from-zero-alt
(lambda (numbers)
(display (list 'furthest-from-zero-alt numbers)) (newline)
(if (null? (cdr numbers))
(car numbers)
(if (>= (abs (car numbers))
(abs (furthest-from-zero-alt (cdr numbers))))
(car numbers)
(furthest-from-zero-alt (cdr numbers))))))
Observation: While there were 5 total calls in (furthest-from-zero
'(5 4 3 2 1)), there were about 3 in `(furthest-from-zero ‘(1 2 3 4 5)).
Problem: In the latter case, each call to the procedure generates two identical recursive calls (one for the test, one for the result). And each of those recursive calls generates two. And so on and so forth.
Moral: Two identical recursive calls are expensive. Avoid repeated code.
A solution: Name the recursive result using let.
(define furthest-from-zero-alt
(lambda (numbers)
(display (list 'furthest-from-zero-alt numbers)) (newline)
(if (null? (cdr numbers))
(car numbers)
(let ([result-from-magic-fairy
(furthest-from-zero-alt (cdr numbers))])
(if (>= (abs (car numbers))
(abs result-from-magic-fairy))
(car numbers)
result-from-magic-fairy)))))
DrRacket tips and tricks
Or: Watch Sam have difficulty with DrRacket.
Reading summary
Missed.
Lab
Whoops. (min 6.33 0) => 0.0
Writeup: 3de. No documentation necessary.
Debrief (and more)
Note: Finding common approaches and representing them somewhere is useful.
Note: To reduce, you need a procedure that returns the same type as
the parameters. So, you can’t find the smallest using <, since that takes
two numbers and returns #t/#f. You find the smallest using min
or (lambda (a b) (if (<= a b) a b)).
Friday’s quiz! I will give you pictures of everyone in class