Warning This class is being recorded (and transcribed), provided Sam remembered to hit the “Record” button.
Approximate overview
Academic
Cultural
Peer
Wellness
Misc
Someone will be here at 7:30 a.m. on Friday so that those who want to take multiple quizzes can do so.
Describe or diagram the layout of memory for lists, pairs structures, and vectors/arrays.
Draw a box-and-pointer diagram for the following structure, named stuff.
(define stuff
(list (cons 'a 'b)
(cons 'c (cons 'd 'e))
(cons (cons null 'f) 'g)))
You can read some more pair problems on the LAs page.
Design and write recursive functions over the natural numbers.
Write a recursive procedure, (bounded-power-of-2 n), that finds the largest integer power of 2 less than of equal to the positive integer n.
(check-equal? (bounded-power-of-2 1)
1
"edge case/base case: 2^0")
(check-equal? (bounded-power-of-2 2)
2
"edge case/base case: 2^1")
(check-equal? (bounded-power-of-2 3)
2
"normal case: small non-power-of-two")
(check-equal? (bounded-power-of-2 7)
4
"normal case: small non-power-of-two")
(check-equal? (bounded-power-of-2 16)
16
"normal case: relatively small power of 2")
(check-equal? (bounded-power-of-2 17)
16
"normal case: relatively small non-power-of-two")
(check-equal? (bounded-power-of-2 72)
64
"normal case: somewhat larger non-power-of-two")
(check-equal? (bounded-power-of-2 (expt 2 123))
(expt 2 123)
"edge case: large power of 2")
(check-equal? (bounded-power-of-2 (+ (expt 2 123) 123))
(expt 2 123)
"edge case: large non-power of 2")
As always, SamR recommends that you review the lab and reading to prepare for the quiz.
Could you go over the self-checks?
The way we have drawn pair structures above makes it easy to think about
carandcdroperation. Reading from the inside out, you simply follow the arrow from the left side of the pair forcaror the arrow from the right side of the pair forcdr.
a. Using this strategy, find the values corresponding to the following commands applied to the structure repeated below.
caarcadrcaaddr
b. Using an analog of the visual strategy, what sequence of commands would you need to extract the
'eand'd, respectively?
What do we get if we take (cddddr stuff), which I think gives us a slash.
slash in diagrams is just “null”.
Can you have a pair in which the car is null?
Yes!
Bad ASCII art
+---+---+
| / | / |
+---+---+
That’s the list of null.
Does this mean that Scheme/DrRacket treats all lists as a collection of pairs paired together?
Yes.
We do this reading/discussion/lab so that you have a bit of understanding of what’s going on behind the scenes.
This is our mental model of how data are stored in Scheme.
Are the dots in pairs elements of the list, or are they ignored by procedures like list-ref?
The dots represent “this isn’t a list; it just looks like one”.
Lists are supposed to end in null, things that have dots don’t end in null.
Sample code
(cons ‘a (cons ‘b ‘c)) ‘(a b . c) (list? (cons ‘a (cons ‘b ‘c))) #f (list-ref (cons ‘a (cons ‘b ‘c)) 0) ‘a (list-ref (cons ‘a (cons ‘b ‘c)) 1) ‘b (list-ref (cons ‘a (cons ‘b ‘c)) 2) . . list-ref: index reaches a non-pair index: 2 in: ‘(a b . c) (length (cons ‘a (cons ‘b ‘c))) . . length: contract violation expected: list? given: ‘(a b . c)
I still don’t get this pair recursion, it is so different than list recursion.
In list recursion, we typically recurse only on the cdr.
In pair recursion, we recurse on both the car and the cdr.
We’ve already done a bit of recursing on the car on MP4.
Is using pairs more efficient in terms of processing time and memory than lists?
I suppose it depends on how you use them. But lists are just pairs, so in most cases there’s no difference.
The goal in learning this is to better understand what’s going on “behind the scenes”.
Can I have until Sunday for MP3 redo?
Yes.
Can you fix the due date for MP4 redo?
Yes.
Does the student who asked deserve people’s applause for asking?
Apparantly.
Does the first fractal problem ask me to make fractals, or just three stacked triangles?
Fractals. You just don’t have to worry about recoloring them, or using different shapes, or …
Can you go over 2e?
If we think about the fractal carpets, we should have a mental image of what happens to each square
+---+---+---+
| | | |
+---+---+---+
| | | |
+---+---+---+
| | | |
+---+---+---+
Options:
- X: We can recurse, keeping the same primary color and secondary color. (black is the primary in our examples, red is the secondary)
- Y: We can recurse, swapping the primary and secondary.
- x: We can just draw a square in the primary.
- y: We can just draw a square in the secondary.
Our goal is to write a string describing the pattern.
+---+---+---+
| X | y | X |
+---+---+---+
| x | Y | x |
+---+---+---+
| X | y | X |
+---+---+---+
Our call would be
(carpet "XyXxYxXyX" 128 (rgb 0 0 0) (rgb 255 0 0) 3).
Can we talk about new-rgb-fractal-triangle
Yes, the recursive portions should have redder, greener, and bluer central triangles, respectively.
It may be that Sam’s example was not correct. Stay tuned. Thank you student for noticing.
_Yay! We have a whole 30 minutes for lab.”