Warning This class is being recorded.
Approximate overview
Academic
Cultural
Peer
Wellness
Misc
Utilize boolean expressions and values in a program to produce conditional behavior.
As you know, we often associate words with students’ standing in college. At Smileyville College, where we have three terms per year, those who have completed fewer than four terms are “freshlings”, those who have completed between four and seven terms (inclusive) are “wise fools”, those who have completed between eight and eleven terms (inclusive) are “subordinates”, and those who have completed at least twelve terms are “elders”.
Write a procedure, (status terms), that, given the number of
terms a student has completed as input, produces a string that
describes the student using the words above.
TPS
Which do you prefer to generate '(6 5 4 3)?
(take (reverse (range 7)) 4)(reverse (drop (range 7) 3))(take (drop (reverse (range 10)) 3) 4)Comments
reverse will be faster on shorter lists.drop is usually faster than take (given the same n).What’s wrong with the following solution to the self-check?
(define rainbow-colors
'("red" "orange" "yellow" "green" "blue" "indigo" "violet"))
;;; (outlined-circle color) -> image?
;;; color : color?
;;; Make an outlined circle of the specified color.
(define outlined-circle
(lambda (color)
(overlay (circle 20 "outline" "black")
(circle 20 "solid" color))))
;;; (color-darker c) -> color?
;;; c : color?
;;; Create a slightly darker version of c.
(define color-darker
(lambda (c)
(rgb (- (color-red (color-name->rgb c)) 32)
(- (color-green (color-name->rgb c)) 32)
(- (color-blue (color-name->rgb c)) 32))))
#|
b. Using apply and map, make a picture of seven outlined circles in darker versions of the rainbow colors. Note that you’ll need to convert the color names to RGB colors with color-name->rgb and then make the darker with two calls to color-darker.
|#
(define rainbow-colors
'("red" "orange" "yellow" "green" "blue" "indigo" "violet"))
(apply beside (map outlined-circle (map color-darker rainbow-colors)))
color-darker requires RGB colors.
(Well, it was rewritten, but it’s BAD IDEA to rewrite Sam’s code,
PARTICULARLY if you don’t change the documentation.)color-darker”.What would Sam have liked?
(define darker-rainbow-circles
(apply beside
(map outlined-circle
(map color-darker
(map color-darker
(map color-name->rgb
rainbow-colors))))))
Observation: You need to learn to read Scheme expressions inside-out.
It’s okay if you don’t try the code, but then let me know that you’re not sure whether or not it works.
How about this one?
(define 7-Circle-darker
(apply beside (map outlined-circle (map color-darker
(list
(color-name->rgb "red")
(color-name->rgb "orange")
(color-name->rgb "yellow")
(color-name->rgb "green")
(color-name->rgb "blue")
(color-name->rgb "indigo")
(color-name->rgb "violet")
)))))
color-name->rgb seven times, we could use map.Ask questions. It’s part of self gov. If you have questions, others likely do, too.
Why do I sometimes get '(1 2 3) and sometimes (list 1 2 3)?
I wish I knew. That behavior is new to DrRacket this year.
Just assume there are multiple ways to say “This is a list”.
map and applyWhy do we need map and apply? Why can’t I just write (+ (list 1 2 3))?
The designers of Scheme decided that it was confusing to have
+take both individual values and lists as parameters.+takes only individual values.
It’s also likely to be less efficient if we try to make all procedures accept a list. Then the system needs to check if the parameter is a list or not.
Some cases are also likely to be ambiguous, such as when we have a procedure that accepts lists as input.
Do I want
(list '(1 2 3))to make a list of the list'(1 2 3)or a list of the values1,2, and3?
I’m not sure how else we’d write
map.
Going over a bit more on map/apply when more than one lists are given would be helpful.
applytakes one only list.
We’ll explore multi-parameter
mapin the lab.
(map circle (list 10 20 30) (make-list 3 "solid") (list "red" "green" "blue"))
--> (list (circle 10 "solid" "red")
(circle 20 "solid" "green")
(circle 30 "solid" "blue"))
--> (list SOLID-RED-CIRCLE
(circle 20 "solid" "green")
(circle 30 "solid" "blue"))
--> (list SOLID-RED-CIRCLE
SOLID-GREEN-CIRCLE
(circle 30 "solid" "blue"))
--> (list SOLID-RED-CIRCLE
SOLID-GREEN-CIRCLE
SOLID-BLUE-CIRCLE
How would you like us to do the colorful-circles exercise? I feel
like I could have done it better.
See above.
Could you give me an example of a trace?
See above.
Can we round angles for HSV to exact integers?
Yes, that’s a great idea.
Person closest to the board is A. Person furthest from board is B.
char->digit(char->integer #\0) -> 48(char->integer #\1) -> 49(char->integer #\2) -> 50(char->integer #\3) -> 51(char->integer #\4) -> 52(char->integer #\5) -> 53Do you see a pattern?
string->integerThink about this sequence of values for the input “8143”.
"8143"'(#\8 #\1 #\4 #\3)'(8 1 4 3)'(8000 100 40 3)8143We are decomposing this problem in two different ways:
map.It’s 9:45. “SAM SAID I CAN STOP HERE”
But please finish the problems; they are important.