Please follow the standard start-of-class routine.
Warning! You are being recorded (and transcribed) (provided the technology is working correctly).
Approximate overview
Scholarly
Artistic/Cultural
Multicultural
Peer
Musical, theatric, sporting, academic, and similar events involving this section’s students are welcome.
Wellness
Misc
These do not earn tokens, but are worth your consideration.
CHANGES.txt that describes what you’ve changed.The submission for the first redo deadline is free. Remaining redos are one token each.
If you didn’t turn in the assignment for the original deadline (or its late deadline), the redo is free, but you are charged two tokens for failing to turn in the original. (This policy holds starting with MP2.)
If you don’t turn in a redo for the first redo deadline (or up to two days late with a token), you will be charged a token for each remaining redo deadline, even though they may be your “first” redo.
Please make sure that your redo is accompanied by a CHANGES.txt file that
summarizes the changes you’ve made. For example,
eye procedure to part 2 because I had neglected to
include a procedure for that part. I use it in making my cat.my-image was a procedure. It is now an image, as the assignment requested.char->digitMany of you wrote
(define char->digit
(lambda (char)
(- (char->integer char) 48)))
I’d prefer that you write
(define char->digit
(lambda (char)
(- (char->integer char) (char->integer #\0))))
Can you tell why?
48 is a “magic number”. That is, it’s a number in your code that the reader may not necessarily understand. A reader can tell what’s happening better in the second version than the first. [+2]
It will also work no matter what the collating sequence is, provided the collating sequence numbers the digits in order.
I might even prefer
(define char->digit
(o (r-s - (char->integer #\0))
char->integer))
string->integerSome of you asked that we go over the last question from the lab.
b. Write a procedure,
(concentric-palette colors), that takes a list of colors as a parameter and makes an image of concentric squares of sizes 20, 30, 40, … (* 10 (length colors)).
We should decompose the problem a bit. I’ll try to ask the kinds of questions one might ask in developing a solution to the problem. YMMV.
I need to make a square. How do I do that?
(solid-square size color)
If I wanted to make squares of the same size (20), what procedure would I use to make such squares?
(define square-20 (lambda (color) (solid-square 20 color)))
How can I use that to make a size-20 square for each color in the list?
(map square-20 (list "red" "orange" "yellow" "green" "blue"))
Do I have a shorter way to write the lambda expression?
(map (l-s solid-square 20) (list "red" "orange" "yellow" "green" "blue"))
However, we need the sizes to vary, too. How can we get a list of the sizes?
(list 20 30 40 ....) What procedure gets us a range of numbers?
range.
We might start with (range (length colors))
How do I go from (list 0 1 2 3 ...) to (list 20 30 40 ...)?
Multiply each by 10 and add 20.
How do I write a procedure that multiplies by 10 and adds 20?
(o (r-s + 20) (l-s * 10))
Putting stuff together, we get
(define palette
(lambda (colors)
(map solid-square
(map (o (r-s + 20) (l-s * 10))
(range (length colors)))
colors)))
We need to stack them, so we can use apply overlay.
(define palette
(lambda (colors)
(apply overlay
(map solid-square
(map (o (r-s + 20) (l-s * 10))
(range (length colors)))
colors))))
map“I have to add 2 to each element of a list.”
(map (l-s + 2) lst)
(define add2
(lambda (val)
(+ 2 val)))
(map add2 lst)
“I have to add 2 to each element of a list and then multiply each element by 10.”
(map (o (l-s * 10) (l-s + 2)) lst)
(define something
(lambda (num)
(* 10 (+ 2 num))))
(map something lst)
(map #(* 10 (+ 2 %1)) lst)
Can we trace (map (o (l-s * 10) (l-s + 2)) lst)
(map (o (l-s * 10) (l-s + 2)) (list 0 1 2 3))
--> (list ((o (l-s * 10) (l-s + 2)) 0)
((o (l-s * 10) (l-s + 2)) 1)
((o (l-s * 10) (l-s + 2)) 2)
((o (l-s * 10) (l-s + 2)) 3))
; Note ((o f g) val) --> (f (g (val)))
--> (list ((l-s * 10) ((l-s + 2) 0))
((o (l-s * 10) (l-s + 2)) 1)
((o (l-s * 10) (l-s + 2)) 2)
((o (l-s * 10) (l-s + 2)) 3))
; Note ((l-s fun left) right) --> (fun left right)
--> (list ((l-s * 10) (+ 2 0))
((o (l-s * 10) (l-s + 2)) 1)
((o (l-s * 10) (l-s + 2)) 2)
((o (l-s * 10) (l-s + 2)) 3))
--> (list ((l-s * 10) 2)
((o (l-s * 10) (l-s + 2)) 1)
((o (l-s * 10) (l-s + 2)) 2)
((o (l-s * 10) (l-s + 2)) 3))
--> (list (* 10 2)
((o (l-s * 10) (l-s + 2)) 1)
((o (l-s * 10) (l-s + 2)) 2)
((o (l-s * 10) (l-s + 2)) 3))
--> (list 20
((o (l-s * 10) (l-s + 2)) 1)
((o (l-s * 10) (l-s + 2)) 2)
((o (l-s * 10) (l-s + 2)) 3))
...
Will redos be available for the LAs that I’m still missing?
Yes. Redos for each LA will be available until everyone has passed that LA or we reach the end of the semester.
There’s an estimate of the number of classes I missed. Will I be charged tokens for missing those classes?
If I’ve charged you tokens for missing classes, you’ll see them under “Attendance” in the tokens section.
What determines how many semicolons you use at the start of a line?
We use three semicolons to mark the documentation for individual procedures or values.
We use two semicolons for broader comments for the reader.
We use one semicolon either to “comment out” code that is not yet ready to run or to insert comments in the middle of a procedure.
Why is and a keyword and not a procedure? I didn’t follow along
well with the given example.
We have a standard approach to evaluating procedure calls (function calls): Evaluate all of the arguments and then apply the function.
anddoesn’t behave that way. It evaluates its parameters one by one, making it similar to a function, but different.
Could you please explain further the difference between apply and reduce? How do we know which one to use?
Apply works with any function and a list that contains exactly as many values as the number of arguments the function expects. It calls the function on those values.
(apply fun (list a b c d e))->(fun a b c d e).
Reduce works with a binary procedure and a list of any length. It repeatedly replaces pairs of neighboring elements by the result of applying the procedure until we’re down to one element.
(reduce fun (list a b c d e))->(fun (fun a b) (fun c (fun d e)))
(reduce-left fun (list a b c d e))->(fun (fun (fun (fun a b) c) d) e)
(reduce-right fun (list a b c d e))->(fun a (fun b (fun c (fun d e))))
Some functions, like
+andstring-appendbehave the same way when used with eitherreduceorapply. In part, that’s because they can take a variable number of parameters. In part, that’s because their semantics are the same either way.
Other functions, like
list, may take an arbitrary number of parameters, but do not behave the same way if we do things pair-by-pair.
(apply list (list 1 2 3 4 5))->(list 1 2 3 4 5).
(reduce list (list 1 2 3 4 5))->(list (list (list (list 1 2) 3) 4) 5)).
Can you go over self-check 2b from Monday?
Sure!
b. Using
applyandmap, make a picture of seven outlined circles in darker versions of the rainbow colors (using two calls torgb-darker). Note that you’ll need to convert the color names to RGB colors withcolor-name->rgband then make them darker with two calls torgb-darker.
Note: You might need three or four calls to
map(or a particularly good composition of functions).
(define rainbow-colors (list "red" "orange" "yellow" "green" "indigo" "violet"))
(map color-name->rgb rainbow-color) ; gives a list of color names
(map rgb-darker (map rgb-darker (map color-name->rgb rainbow-color)))
(define thickly-outlined-circle
(lambda (color)
(overlay (outlined-circle 20 "black" 5)
(solid-circle 20 color))))
(map thickly-outlined-circle (map rgb-darker (map rgb-darker (map color-name->rgb rainbow-color))))
(map (o thickly-outlined-circle rgb-darker rgb-darker color-name->rgb)
rainbow-colors)
Don’t forget to reload Scamper to ensure that you are using 4.7.0.
The Scheme file should be called list-more.scm. If it’s not, go back to
the schedule, reload, click on the lab link, and cross your fingers.