CSC 151.01, Class 24: Recursion Basics, Continued
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Friday PSA
- Questions
- Quiz
- Finding the largest element, revisited
- Lab
News / Etc.
- Continue partners!
- Happy Friday. I brought you food.
- Thank you to those of you who raised important issues on Wednesday (Thank you also to those who made the statement that we fight the power through more learning.)
Rotating reminders
- Get news! Feel free to ask me to sign you up for the department mailing list and get the daily Spam from Sam.
Upcoming Work
- Exam 2 prologue due TONIGHT.
- Exam 2 due next Tuesday.
- Lab writeup: TBD
- Reading: Preconditions, Revisited
Extra credit (Academic/Artistic)
- CS Table, Tuesday at noon, Generating novels and poetry
- CS Extras, Thursday at 4:15 pm, The CS curriculum
- Up to two of the events in the Rosenfield Technology and Human Rights symposium
Extra credit (Peer)
- Pun club, Saturday, 4:00 p.m., Younker lounge
- Playboy of the Western World, next weekend (9th-12th).
- Test Ritalin Squid next Friday the 10th
- G-Tones concert, March 12 with Opposed to Toy Trains, time tbd
- Grinnell Singers, March 12 at 2pm
Extra credit (Misc)
Good things to do
- High school play this weekend
Friday PSA
- I brought you a present of Blood Alcohol Content cards
- They are not meant as a challenge
- Moderation in everything (even CS exams)
- Take care of yourself and those around you
- Be responsible to your partner
Questions
- Should I use
display
on problem 1? - The answer can be found in times 0:11 to 0:22 of https://www.youtube.com/watch?v=Pg0VK_jjiRw
- No.
- What should I do instead?
- You can just type a value to get that value.
(define type-of
(lambda (val)
(if (procedure? val)
'procedure
'something-other-than-a-procedure)))
- Why do you invert horizontal and vertical in describing blends?
- I don’t.
- Imagine Sam’s hand blending horizontally or vertically or diagonally or daemonically or whatever
Can we use let
and let*
in anonymous procedurees and can you do
an example?
(image-show
(image-variant (image-load "/home/rebelsky/Desktop/zachary.jpg")
(lambda (color)
(let ([average (* 1/3 (+ (irgb-red color)
(irgb-green color)
(irgb-blue color)))])
(irgb average average average)))))
- Can you explain the periods in the lists?
- Interpret those as “not quite a list; you did something wrong if you thought you were making a list”
- How do you accidentally make a non-list
- I want to make the list
'(a b c)
. I use(cons 'a (cons 'b 'c))
. - We should have put a null at the end.
(cons 'a (cons 'b (cons 'c null)))
- What’s the difference between
append
andcons
? append
joins two lists together.cons
shoves something on the front of a list (therby creating a new list)
> (cons 'a (list 1 2 3))
'(a 1 2 3)
> (append 'a (list 1 2 3))
. . append: contract violation
expected: list?
given: 'a
> (append (list 'a 'b) (list 1 2 3))
'(a b 1 2 3)
> (cons (list 'a 'b) (list 1 2 3))
'((a b) 1 2 3)
Quiz
Finding the largest element, revisited
Here’s what we had at the end of the last class.
(define largest
(lambda (lst)
(if (only-one-element? lst)
lst
(max (car lst) (largest (cdr lst))))))
; Normal technique: Simplify, delegate, and use result
(define only-one-element?
(lambda (lst)
(= (length lst) 1)))
(define only-1-element?
(lambda (lst)
(and (not (null? lst)) (null? (cdr lst)))))
Current questions
- Are we comfortable with the base case?
- What should we do in the recursive case?
- Why doesn’t Sam like the first version of
only-one-element?
(define largest
(lambda (lst)
(if (only-one-element? lst)
(car lst)
(max (car lst) (largest (cdr lst))))))
(define only-one-element?
(lambda (lst)
(= (len lst) 1)))
(define only-1-element?
(lambda (lst)
(and (not (null? lst)) (null? (cdr lst)))))
(define len
(lambda (lst)
(display "Computing ") (display (list 'len lst)) (newline)
(if (null? lst)
0
(+ 1 (len (cdr lst))))))
Writeup
This writeup is optional!
Write up exercise 4 from the lab on recursion basics. Include 6P-style documentation.
;;; Procedure:
;;; product
;;; Parameters:
;;;
;;; Purpose:
;;; Compute the product of ...
;;; Produces:
;;; result, a ...
;;; Preconditions:
;;;
;;; Postconditions:
;;;
Send your solution to csc151-01-grader@grinnell.edu.
Title your email CSC 151.01 Writeup for Class 24 (YOUR FULL NAMES).