Warning! You are being recorded and transcribed, provided the technology is working correctly.
Approximate optimistic overview
Scholarly
Artistic
Multicultural
Peer
Musical, theatric, sporting, and academic events involving this section’s students are welcome.
Wellness
Misc
These do not earn tokens, but are worth your consideration.
We’re skipping recursion questions for now.
When can I make up the Documentation LA?
On SoLA 2. (or SoLA 3, 4, 5)
Suppose I don’t do well on the lists LA. When can I make that up?
On SoLA 2. (or SoLA 3, 4, 5)
I’m struggling with how to get part 2 to work. Any hints?
You’ll need to use some of the same ideas as in part 1. In particular, you’ll find it helpful to write a helper procedure that checks whether its parameter is a shape or not and does different things based on that test.
Tell me more about the doubly-nested lists.
The elements of doubly-nested lists are either (a) basic shapes or (b) singly-nested lists of basic shapes.
The elmeents of singly-nested lists are either (a) basic shapes or (b) lists of basic shapes.
(list (solid-square 30 "blue")
(list (solid-square 30 "green"))
(list (solid-circle 100 "orange")
(list solid-square 10 "yellow")))
What’s the difference between a substring function that takes a inclusive 0-based index as start and an exclusivee 0-based index as end parameters, and a substring function that takes a inclusive 1-based index for start and an inclusive 1-based index for end parameters?
In Scheme, we almost always use 0-based indexing. So you shouldn’t worry about 1-based indesing.
In Scheme, we almost always use “lower-bound inclusive; upper-bound exclusive”.
(substring "Hello" 1 3)
->"He"
Let’s talk.
Recursion is a technique for writing algorithms that need to do repeated work.
One key idea is similar to that of decomposition.
if there are no cards in the stack
return 0
otherwise
remove a card
ask your assistant to count the cards in the modified stack
add 1 to what your assistant says and return that number
We’ve figured out how length
works!
if there are no cards in the stacka
return 0
otherwise
remove a card
ask your assistant to count the number of cards that end with a one in the rest
if your card ends with zero
return the count from your assistant
if your card ends with a one
add one to the count from your assistant
We’ve just implemented tally
if there are no cards the stack
return the empty stack
otherwise
keep one card
ask your assistant to find the cards that end with zero in the rest
if your card ends with zero
add your card to the stack you got from your assistant and pass it on
to the next person
otherwise
return the stack that you got from your assistant
We’ve just implemented filter
.
(define my-length
(lambda (lst)
(if (null? lst)
0
(+ 1 (my-length (cdr lst))))))
It works! Yay!
What if we forgot the cdr? It runs forever! (Well, until the computer crashes.)
;;; (tally-odds ints) -> integer?
;;; ints : (list-of exact-integer?)
;;; Count the number of odd numbers in the list.
(define tally-odds
(lambda (lst)
(if (null? lst)
0
(if (odd? (car lst))
(+ 1 (tally-odds (cdr lst)))
(tally-odds (cdr lst))))))
A slightly better answer
;;; (tally-odds ints) -> integer?
;;; ints : (list-of exact-integer?)
;;; Count the number of odd numbers in the list.
(define tally-odds
(lambda (lst)
(cond
[(null? lst)
0]
[(odd? (car lst))
(+ 1 (tally-odds (cdr lst)))]
[else
(tally-odds (cdr lst))])))
(define select-even
(lambda (lst)
(if (null? lst)
null
(if (even? (car lst))
(cons (car lst)
(select-even (cdr lst)))
(select-even (cdr lst))))))