Approximate overview
None right now.
(define play-seven-eleven
(lambda ()
(cond
[(= 7 (pair-a-dice))
1]
[(= 11 (pair-a-dice))
1]
[else
0])))
(define pair-a-dice
(lambda ()
(+ (roll-a-die) (roll-a-die))))
(define roll-a-die
(lambda ()
(random 1 7)))
(define count-wins
(lambda (n)
(cond
; No games -> No wins
[(zero? n)
0]
; We win (-: Count it and move on
[(= (play-seven-eleven) 1)
(+ 1 (count-wins (- n 1)))]
; We lose )-: Just move on
[(= (play-seven-eleven) 0)
(count-wins (- n 1))]
; ??? This should never happen
[else
(error "Tie!")])))
(define count-wins
(lambda (n)
(if (zero? n)
0
(+ (play-seven-eleven)
(count-wins (- n 1))))))
8/36 rolls win. So when we play a lot of games, we should win about 8/36 of the time. If we play 3600 games, we should win approximately 800 times. Do we?
play-seven-eleven. Be careful about
extra calls to recursive procedures.random neither too few nor too many times.pair-a-dice is one of the best procedure names ever.Can we use or to solve the seven-eleven problem?
No.
Can we use a more sophisticated version of or that we don’t learn in 151?
Perhaps.
;;; (random-list-element lst) -> any?
;;; lst : listof any?
;;; Randomly select an element of `lst`
(define random-list-element
(lambda (lst)
(list-ref lst (random (length lst)))))
;;; people -> listof string?
;;; A list of some of the folks who teach 151
(define people (list "Peter-Michael" "Nicole" "Sarah" "SamR" "Barbara" "Priscilla" "Jerod"))
;;; (random-person) -> string?
;;; Randomly select an element of the people list.
(define random-person
(lambda ()
(random-list-element people)))
Why does random-person have a lambda with no parameters?
We don’t need the list of people, since that’s already defined. (
random-list-elementonly needs one parameter)
Why don’t I just write
(define random-person (random-list-element people)).
Putting in the lambda ensures that we re-do the
random-list-elementeach time the procedure is called.
We don’t need any inputs (see above), so we leave the parameters blank.
Are there other cases in which we’d need an empty lambda?
Not normally. If we were to do more sophisticated file I/O, we might.
Our goal: Generate some interesting language (Haiku and Templated Writing).
Consider the following procedures
;;; (vowel? char) -> boolean
;;; char : char?
;;; Determine if char is a vowel.
(define vowel?
(let ([vowels (string->list "aeiou")])
(lambda (ch)
(integer? (index-of vowels (char-downcase ch))))))
;;; (count-vowels str) -> integer?
;;; str : string?
;;; Count the number of vowels in str
(define count-vowels
(lambda (str)
(tally vowel? (string->list str))))
;;; (select-special-words words) -> list-of string?
;;; words : list-of string?
;;; Selects all the special words in words using the ALTV criterion.
(define select-special-words
(lambda (words)
(filter (o (section > <> 2) count-vowels) words)))
a. What kinds of words does select-special-words select?
Words that have more than two vowels.
b. Explain how (o (section > <> 2) count-vowels) works as a
predicate for such words.
First it counts the vowels in the word and then compares that number to two.
(section > <> 2)is a Scheme way to write “greater than two”
c. Rewrite vowel? using section and composition but no
lambda.
Old
(define vowel?
(let ([vowels (string->list "aeiou")])
(lambda (ch)
(integer? (index-of vowels (char-downcase ch))))))
New
Approach
vowel? procedure?
string-downcaseindex-of to find it in the list of vowelsinteger? to see if index-of found it(define vowel?
(let ([vowels (string->list "aeiou")])
(o integer? (section index-of vowels <>) char-downcase)))
Morals:
o to build the sequence.section to fix
some of those parameters and give you a one-parameter procedure.Why didn’t we need section for char-downcase?
char-downcaseonly takes one parameter. We usesectionwhen we’re trying to reduce the number of parameters.
Where is the lambda?
oadds an implicit lambda
(o f g)–>(lambda (x) (f (g x)))
You are unlikely to receive a problem this hard.
Consider the following procedure.
(define silly
(lambda (lst)
(map (lambda (x) (sqr (+ 1 x)))
(filter odd? lst))))
Rewrite the procedure using o and section so that it has no lambdas.
o to sequence those two.(define silly
(o (section map (lambda (x) (sqr (+ 1 x))) <>)
(section filter odd? <>)))
That procedure adds one and then squares. We can write that as
(o sqr (section + 1 <>)).
Putting it together
(define silly
(o (section map (o sqr (section + 1 <>)) <>)
(section filter odd? <>)))