CSC 151.01, Class 51: Even More Discussion of Exam 3
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Questions
- Preconditions (with problem 5)
- Husk and kernel (with problem 5)
- Vector recursion (with problem 6)
- Deep recursion (with problem 4)
News / Etc.
- We will use today’s class to discuss exam 3.
- New partners!
- Exam 4 distributed.
- No morning review session tomorrow. 7 p.m. review session will be open-ended (a chance to ask questions related to the exam). 8 p.m. will be for the other section.
Upcoming Work
- NO lab writeup! (In fact, we have no more lab writeups.)
- Reading for Friday: Search algorithms
- Make sure to do the self-check!
- You’ve probably done it already, right?
- No quiz Friday.
Extra credit (Academic/Artistic)
- IMPORTANT: CS Extras, Thursday, May 4, 4:15 p.m.: Inclusion in CS. Science 2022. Please attend. Snacks available at 4:00 p.m. Bonus extra credit!
- 5th annual Break open the vault, 8pm, Thursday, Special Collections.
Extra credit (Peer)
- This Thursday, May 4, the Grinnell College Jazz Ensemble will STOP TRAFFIC. Literally. The city (with support from Chuong Garden) has agreed to close Broad Street between 4th and 5th for our end-of-semester concert. From 7-8pm, they’ll be playing “brave music for troubled times” on the steps of the Arts Center (926 Broad), including tunes by Marvin Gaye (“What’s going on?”), George Michael (“Freedom ‘90”), Charles Mingus (“Thursday Night Prayer Meeting”), Grinnell’s own Aaron Israel Levin (“pasaquaglia, but in an Italian way”) and Kendrick Lamar (“Alright”).
- Grinnell Monologues is performing on this Thursday and Friday in Main Lounge at 7:30 p.m.
- Drill Team Show, Friday, May 5, at Triple V stables. The other side of the golf course. Free rides (cars, not pony) to the event!
- AAA/SOL/CBS Open Mike in Bob’s Friday, 7-9pm, $200 tab.
- Swim Team hosts Triathalon this weekend (running 3 miles,
biking 13 miles, swimming 1/2 mile). Participate or help out.
- Sam will reimburse your admission fee.
- GSEA (Grinnell Space Exploration Agency) is launching a weather balloon on May 7th at 5:30AM. The balloon will go to almost-space and take pictures. It’ll also tell us about its location and the temperature of the air around it as it rises. We’re inviting people to come see the launch, if they want to come (despite the fact that it’s at 5:30AM).
- Next home baseball game: Sunday, May 7 (Senior Day)
- Advanced Performance Performances Sunday at 7pm in the Wall
Extra credit (Misc)
- May 4 town hall on belonging. 11am, May 4, JRC 101. It sounds like we have some important things to discuss; please come share your perspectives!
Other good things to do
- Read about the awesome Kumail Nanjiani ‘01.
- Your major does not have to define you: Kumail was a CS/Philosophy double major.
Questions
Preconditions (with problem 5)
(define sierpinski-carpet-region!
(lambda (image left top width height small-enough)
...
(image-select-rectangle! image REPLACE left1 top1 w/3 h/3)
...))
What are important preconditions of (image-select-rectangle! image
OPERATION left top width height)?
;;; Procedure:
;;; image-select-rectangle!
;;; Parameters:
;;; image, an image
;;; OPERATION, a symbol
;;; left, a real number
;;; top, a real number
;;; Preconditions:
;;; left and top should be nonnegative
;;; width and height should be positive (> 1)
;;; OPERATION has to be a valid operation (REPLACE, ADD, SUBTRACT, INTERSECT)
;;; width and height should be no bigger than the image
;;; left and top should be within the image
;;; 0 <= left < (image-width image)
;;; 0 <= top < (image-height image)
What types should the parameters of `sierpinksi-carpet-region! be?
What additional preconditions should we add to ensure that the
calls to image-select-rectangle are valid?
;;; Procedure
:;; sierpinski-carpet-region!
;;; Parameters:
;;; image, an integer that represents a valid image [verified]
;;; left, a non-negative real number [verified]
;;; top, a non-negative real number [verified]
;;; width, real number > 1 [verified]
;;; height, real number > 1 [verified]
;;; small-enough, a positive real number [verified]
;;; Preconditions:
;;; (+ left width) <= (image-width image) [verified]
;;; (+ top height) <= (image-height image) [verified]
;;; ...
- Note: If you say
number, I’ll assume that a complex number is acceptable. - If the image is 100x100, left and top are 0 and 0, width and height are
3000 and 3000, what rectangle do we select for the “center portion”?
- Start at 1000,1000, width 1000, height 1000
- Really big.
- Not on the image! Violated the preconditions of
image-select-rectangle!
Husk and kernel (with problem 5)
What is husk-and-kernel programming?
- Two separate procedures / portions thereof
- Husk checks preconditions (protects the kernel/the interesting part)
- The kernel does the real work.
What form does it normally take?
(define proc
(lambda (x params)
(cond
[(precondition-one-test? params)
(error "Did you not read the documentation?")]
[(precondition-two-test? params)
(error "Do you even know how to read?")]
[else
(kernel ...)])))
What should we check in the husk of sierpinski-carpet-region!?
(define sierpinski-carpet-region!
(lambda (image left top width height small-enough)
(cond
[(not (image? image))
(error "image is not an image")]
[(not (real? left))
(error "left is not a real number")]
...
[(< left 0)
(error "left must be nonnegative")]
...
[(not (<= (+ left width) (image-width image)))
(error "The selected area must be within the bounds of the image.")]
[(not (<= (+ top height) (image-height image)))
(error "The selected area must be within the bounds of the image.")]
[else
(kernel ...)])))
Vector recursion (with problem 6) ———————————
What patterns do you know for vector recursion?
(define vector-proc
(lambda (vec)
(let kernel ([pos 0])
(when (< pos (vector-length vec))
...
(kernel (+ pos 1))))))
(define vector-proc
(lambda (vec)
(let kernel ([pos (- (vector-length vec) 1)])
(when (>= pos 0)
...
(kernel (- pos 1))))))
How might they apply to this problem?
(define vmap!
(lambda (proc vec)
(let kernel ([pos (- (vector-length vec) 1)])
(when (>= pos 0)
(vector-set! vec pos (proc (vector-ref vec pos)))
(kernel (- pos 1))))))
- Um, something with
vector-refandvector-set!
We can add
(display (list 'vec vec 'pos pos)) (newline)
What’s the moral?
- If you identify good patterns, many things should be easier.
Deep recursion (with problem 4)
When should we (or should we not) draw the trunk?
After drawing the trunk, what should we do?
Is there a procedure that we can use to draw a branch?
How do the parameters differ for our call to that procedure?
Suppose we had to undo everything. How do you undo a move forward?
Suppose we had to undo everything. How do you undo a turn?
When should we undo our move forward?
When should we undo our turn?