CSC151 2009F, Class 35: Characters and Strings Admin: * Are there any final public questions on exam 2? * Fun event Saturday a.m.: * Grinnell Destination ImagiNation Fun Run/Walk. * More info outside my office. * If you are registered in Poweshiek county, don't forget to vote. * Reading for Wednesday: Vectors. Overview: * Representing text. * Characters: The basic building blocks. * Combining characters into strings. Notes on Exam 2 * Problem 1: How do you figure out if the second thing is the symbol ellipse? * (equal? 'ellipse (cadr val)) * Problem 1: How much do we have to worry about the string? * Just check that it's a string * It's fine if you'd like to check that it's the empty string. * Problem 6: What is this color-distance stuff? * It's a way of measuring the similarity between colors * And yes, you should use rgb-distance in your solution * Problem 8: Should drop-ends work with the empty list? * Probably not * How about a one-element list? * It depends on what preconditions you write * A two-element list? * Yes. From yesterday's class (define pair-a-dice (lambda () (display "Rolling ...") (newline) (+ (roll-a-die) (roll-a-die)))) (define tally-seven-eleven (lambda (n) (cond ((<= n 0) 0) ((or (= (pair-a-dice) 7) (= (pair-a-dice) 11)) (+ 1 (tally-seven-eleven (- n 1)))) (else (tally-seven-eleven (- n 1)))))) * Why the lambda in pair-a-dice? * To make it a procedure (so that it can return different results each time) * But it has no parameters. * What's wrong with tally-seven-eleven? * We potentially roll the dice twice for one check. * How do we fix it? * Use a let to roll the dice and remember the value * Write a separate seven-or-eleven? predicate (define tally-seven-eleven-1 (lambda (n) (cond ((<= n 0) 0) ((let ((roll (pair-a-dice))) (or (= roll 7) (= roll 11))) (+ 1 (tally-seven-eleven-1 (- n 1)))) (else (tally-seven-eleven-1 (- n 1)))))) ; Incorrect - Doesn't reroll the dice (define tally-seven-eleven-2 (let ((roll (pair-a-dice))) (lambda (n) (cond ((<= n 0) 0) ((or (= roll 7) (= roll 11)) (+ 1 (tally-seven-eleven-2 (- n 1)))) (else (tally-seven-eleven-2 (- n 1))))))) ; Incorrect: Counts number of 7's, not number of 7's or 11's. (define tally-seven-eleven-3 (lambda (n) (cond ((<= n 0) 0) ((= (pair-a-dice) (or 7 11)) (+ 1 (tally-seven-eleven-3 (- n 1)))) (else (tally-seven-eleven-3 (- n 1)))))) ; Correct, uses a helpful helper (define tally-seven-eleven-4 (let ((seven-or-eleven? (lambda (roll) (or (= roll 7) (= roll 11))))) (lambda (n) (cond ((<= n 0) 0) ((seven-or-eleven? (pair-a-dice)) (+ 1 (tally-seven-eleven-4 (- n 1)))) (else (tally-seven-eleven-4 (- n 1))))))) Why did you have this problem? * Fun of torturing students * Reinforce the difference between a value and a procedure call * To remind you that procedures sometimes look like they work correctly, but don't * To remind you that each procedure call may impose a cost * To remind you that procedures that rely on random don't always return the same value, so you need to think carefully about whether you want to use a previous result or a new one.