CSC 151.01, Class 24: Randomness and simulation
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Questions
- Lab
- Debrief
News / Etc.
- More breakfast snacks! * Introductory videos.
Upcoming Work
- Quiz Friday
- Recursion
- Named
let letrec
- Writeup for class 23 due TONIGHT at 10:30 p.m.
- Exercises 5d and 6d.
- To: csc151-01-grader@grinnell.edu
- Subject: CSC 151.01 Writeup 23 (YOUR NAMES)
- Writeup for class 24 due Friday at 10:30 p.m.
- Exercise 6.
- To: csc151-01-grader@grinnell.edu
- Subject: CSC 151.01 Writeup 24 (YOUR NAMES)
- Assignment 6 distributed.
- Although the assignment says that you have assigned pairs, I will permit you to choose your own partners (or to work in individuals or trios).
- Read Pairs for Friday’s class.
Extra credit (Academic/Artistic)
- Saving Brinton talk TODAY at 2pm in Harris.
- Saving Brinton, TODAY at 7pm in the Strand.
- Gates Lecture, TODAY at 7:00 p.m. in JRC 101. Professor Sylvester Johnson, Director of the Humanities, Virginia Tech, talk will examine the twentieth-century roots of contemporary national security responses targeting American Muslims as state enemies.
- Convocation TOMORROW (11 am in JRC 101).
- Protest BOT workshop, Friday 4pm in Burling 1st.
Bots are small automated programs that index websites, edit Wikipedia entries, spam users, scrape data from pages, launch denial of service attacks, and other assorted activities, both mundane and nefarious. On Twitter bots are mostly spam, but occasionally, they’re creative endeavors. Mark Sample will lead participants in the creation of bots that can “reveal the injustice and inequality of the world and imagine alternatives. … that question how, when, who and why.”
Extra credit (Peer)
- Women’s Soccer vs. Cornell today at 4 p.m.
- Grinnell Singers Sunday at 2 p.m. in Sebring Lewis.
- Heckling Debate, Thursday, Noyce 2021 at 8pm.
Extra credit (Misc)
- Pioneer Weekend in about two weeks: A one weekend innovation challenge. Registration at https://grinnell.co1.qualtrics.com/jfe/form/SV_0oKEvGlV5twGczr.
Other good things
- Star Wars Music Talk Friday at 4pm.
- Swimming and Diving meet Saturday.
Questions
Lab
count-odd-rolls should have the following form
(define count-odd-rolls
(lambda (n)
(cond
[(zero? n)
0]
[(odd? (roll-a-die))
...]
[else
...])))
Remember: Trust the magic recursion fairy!
Notes on problem 6 (the writeup)
Problem 6a.
- It’s okay to use map-reduce.
- It’s also okay to use recursion.
Problem 6b.
- Generate a random number between 0 and 100
(random 101). - If it’s less than 24, use the first color.
- Generate a random number between 0 and 100
(random 100). - If it’s between 24+… and 24+…+…, use the third color.
- And so on and so forth.
Problem 6c.
- You should be on your way to a pattern for making lists given the length of the list.
Debrief
Fixing tally-seven-eleven
Working but inelegant strategy: Turns numeric recursion into list recursion.
Wastes computer time. Repeats code you already know.
(define pears-a-dice
(lambda (n)
(if (zero? n)
null
(cons (pair-a-dice) (pears-a-dice (- n 1))))))
(define tally-seven-eleven-helper
(lambda (pears)
(cond
[(null? pears)
0]
[(or (= (car pears) 7) (= (car pears) 11))
(+ 1 (tally-seven-eleven-helper (cdr pears)))]
[else
(tally-seven-eleven-helper (cdr pears))])))
(define tally-seven-eleven
(lambda (n)
(tally-seven-eleven-helper (pears-a-dice n))))
If you insist on making lists …
(define seven-or-eleven?
(lambda (val)
(or (= 7 val) (= 11 val))))
(define tally-seven-eleven
(lambda (n)
(length (filter seven-or-eleven? (pears-a-dice n)))))
Less bad, but not great. (Does one extra roll.)
(define tally-seven-elevenn
(lambda (n)
(let ([roll (pair-a-dice)])
(cond
[(zero? n)
0]
[(or (= roll 7) (= roll 11))
(+ 1 (tally-seven-eleven (- n 1)))]
[else
(tally-seven-eleven (- n 1))]))))
Good
(define tally-seven-eleven
(lambda (n)
(if (zero? n)
0
(let ([roll (pair-a-dice)])
(if (or (= roll 7) (= roll 11))
(+ 1 (tally-seven-eleven (- n 1)))
(tally-seven-eleven (- n 1)))))))
Good
(define tally-seven-eleven
(let ([seven-or-eleven? (lambda (roll)
(or (= roll 7) (= roll 11)))])
(lambda (n)
(cond
[(zero? n)
0]
[(seven-or-eleven? (pair-a-dice))
(+ 1 (tally-seven-eleven (- n 1)))]
[else
(tally-seven-eleven (- n 1))]))))