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?
Conceptually, we are subtracting the value of the zero character.
It means the same thing; the second one is clearer. (Perhaps slightly less efficient, but clearer.)
We might end up working in a different collating sequence. Our code will continue to work in a different collating sequence as long as it makes the sensible decision to collate digit characters in order.
I might even prefer
(define char->digit
(o (r-s - (char->integer #\0))
char->integer))
string->integerI hope this served as a useful example of decomposition and/or bottom-up design.
A sample thought process.
Some 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.
How do I make a square?
(solid-rectangle width height color).
(solid-square size color)- the size represents both width and height
Suppose I wanted a list of squares of size 20 (instead of varying sizes). How do I write a procedure that takes a color and makes a single square of size 20?
(define square20 (lambda (color) (solid-square 20 color)))
Next, suppose I wanted a list of squares of size 20. We’ll use
colors as the list we’re working with.
We could use
listand do it individually
(list (square20 (list-ref colors 0)) (square20 (list-ref colors 1)))
We could make this more concise with
(map square20 colors)
Can we do this without separately defining square20?
We could replace
square20by its definition.
(map (lambda (color) (solid-square 20 color)) colors)
(map (l-s solid-square 20) colors)
How can we get the squares to vary in size?
Use
mapwith two lists: One of sizes and one of colors.
(map solid-square (list 20 30 40 50 60) colors)
We need to build the number list programatically. What function might help?
make-listcan make a list of numbers. It takes two inputs: a number that represents the desired size of the list and a second value which represents the element. It then makes a list of that many copies of the same element.
rangewill be useful for making a list of non-identical elements.
(range (length colors))->(list 0 1 2 3 4)
How do I convert (list 0 1 2 3 4) to (list 20 30 40 50 60)?
Use
mapof a function that adds 2 and multiple by 10
(define fun (lambda (n) (* 10 (+ 2 n))))
(map fun (range (length colors)))
Can we do this without defining fun separately?
We could put in the lambda, or we could use cut.
(map #(* 10 (+ 2 %1)) (range (length colors)))
If we preferred section to cut?
(map (o (l-s * 10) (l-s + 2)) (range (length colors)))
Let’s put it together
(define palette
(lambda (colors)
(map solid-square
(map #(* 10 (+ 2 %1)) (range (length colors)))
colors)))
This is close, but we still have a list of squares rather than a single square. What next?
Use
overlay
(define palette
(lambda (colors)
(apply overlay
(map solid-square
(map #(* 10 (+ 2 %1)) (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 function 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 function.
Can we see traces of reduce?
Not currently, but perhaps in the future.
Can I take quizzes tomorrow because I’ll be unavailable on Friday?
Yes, during office hours. You need not book a slot. I’ll hand you a quiz.
Why is and a keyword and not a function? 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"))
(define thickly-outlined-circle
(lambda (color)
(overlay (outlined-circle 20 "black" 5)
(solid-circle 20 color))))
(map thickly-outlined-circle
(map (o rgb-darker rgb-darker) (map color-name->rgb rainbow-colors)))
(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.