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 SoLAs 3, 4, 5)
Suppose I don’t do well on the lists LA. When can I make that up?
On SoLA 2. (Or SoLAs 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.
Recursion bears some similarity to decomposition.
if the stack is empty
return 0
otherwise
remove one card
ask your assistant to count the remaining cards
add 1
This is the algorithm behind length
.
if the stack is empty
return 0
otherwise
remove one card
ask your assistant to count the number of odd remaining cards
if the card is odd
add 1 to the number your assistant returned
otherwise
return the number your assistant returned
This is the algorithm behind: tally
if the stack is empty
return an empty stack
otherwise
remove one card
ask you assistant to grab the odd cards in the remaining stack
if your card is odd
add your card to the stack of odd cards you received and pass the
updated stack along
otherwise
pass the odd cards along, skipping the even card.
This is the algorithm behind: filter
(define my-length
(lambda (lst)
(if (null? lst)
0
(+ 1 (my-length (cdr lst))))))
Yay! We wrote it correctly on the first try.
(define count-odd
(lambda (lst)
(if (null? lst)
0
(if (odd? (car lst))
(+ 1 (count-odd (cdr lst)))
(+ 0 (count-odd (cdr lst)))))))
Yay! We wrote it correctly on the first try.
Except … it can be improved.
(define count-odd
(lambda (lst)
(if (null? lst)
0
(if (odd? (car lst))
(+ 1 (count-odd (cdr lst)))
(count-odd (cdr lst))))))
Or …
(define count-odd
(lambda (lst)
(cond
[(null? lst)
0]
[(odd? (car lst))
(+ 1 (count-odd (cdr lst)))]
[else
(count-odd (cdr lst))])))
(define select-odd
(lambda (lst)
(cond
[(null? lst)
null]
[(odd? (car lst))
(cons (car lst) (select-odd (cdr lst)))]
[else
(select-odd (cdr lst))])))
With a fix to the base case (which needs to be the empty list rather than 0), we got this right, too.