CSC151.01 2015S, Review Session Week 9: Local Procedures and Turtles =================================================================== _Overview_ * Random administrivia * You ask questions, I attempt to answer. * We talk about the forthcoming quiz. Random Administrivia -------------------- _Will there be more than one section on 161 next semester?_ > No. _Can we take 207 without taking 161?_ > No. _Can we take 161 and 207 simultaneously?_ > No. _So what should I do if I want to continue in CS but can't take 161 next semester?_ > Ambitious students teach themselves 161 over the summer using the old curriculum. Talk to me separately about how. > Slightly less ambitious students might talk to the people who are teaching conflicting classes about removing the conflict. Your Questions -------------- _Can we do some tricky questions to get ready for the quiz?_ > What does the following procedure do on an input of '(1 2 3 4 5 6)? (define tricky (lambda (lst) (let kernel ([left null] [right null] [remaining lst]) (if (null? remaining) (list left right) (kernel right (cons (car remaining) left) (cdr remaining)))))) > One attempt to solve left right remaining () () '(1 2 3 4 5 6) () '(1) '(2 3 4 5 6) '(1) '(2) '(3 4 5 6) '(2) '(3 1) '(4 5 6) '(3 1) '(4 2) '(5 6) '(4 2) '(5 3 1)'(6) '(5 3 1)'(6 4 2)'() > Result '((5 3 1) (6 4 2)) > We might make the base case `(list (reverse left) (reverse right))` '((1 3 5) (2 4 6)) > What image do you get from the following? > (define world (image-show (image-new 200 200))) > (define t (turtle-new world)) > (turtle-teleport! t 100 100) > (turtle-face! t 0) > (repeat 5 (lambda (turtle) (turtle-forward! turtle 50) (turtle-turn! turtle 144))) > Write an expression to have a turtle make a regular six-sided star using turtle graphics. You may choose to do two overlapping equilateral triangles or you may choose to just do the outline. (define world (image-show (image-new 200 200))) (define t (turtle-new world)) (turtle-teleport! t 100 100) (turtle-face! t 0) (repeat 6 (lambda () (turtle-forward! t 50) (turtle-turn! t -60) (turtle-forward! t 50) (turtle-turn! t 120))) > Using letrec for the kernel, write a procedure to find the ratio of even to odd numbers in a list of integers. Do ratios using division. (define even-to-odd (letrec ([kernel (lambda (evens odds remaining) (if (null? remaining) _________________ ; a (let ([val (car remaining)]) (if (even? val) _________________ ; b _________________))))]) ; c (lambda (lst) (kernel 0 0 lst)))) > One solution a. (/ evens odds) b. (kernel (+ 1 evens) odds (cdr remaining)) c. (kernel evens (+ 1 odds) (cdr remaining)) > Given that solution, what would a husk that catches inappropriate inputs look like? (lambda (lst) (cond [(not (list? lst)) (error "even-to-odd: expects a list as its parameter, given" lst)] [(not (all integer? lst)) (error "even-to-odd: expects a list OF INTEGERS as its parameter, given" lst)] ... [else (kernel 0 0 lst)])))) > Where is the turtle facing after the following expressions? (turtle-face! t 0) (for-each (lambda (x) (turtle-forward! t 10) (turtle-turn! t (* 15 x))) (iota 5)) _What is the default size of each brush?_ > I don't know. It depends on the brush. If the brushes start with a "2. ", you can set their size. Otherwise, it's whatever the brush designer decided on. _How is `all` defined?_ (define all (lambda (pred? lst) (or (null? lst) (and (pred? (car lst)) (all pred? (cdr lst)))))) > Or maybe (define all (lambda (pred? lst) (let kernel? ([l lst]) (or (null? l) (and (pred? (car l)) (kernel? (cdr l))))))) About the Quiz -------------- Main topics: * Local bindings of recursive procedures * `letrec` * Named let * Turtle graphics * `repeat` * `for-each` * Maybe a combination, but probably not. Local bindings? * Write a husk and kernel (e.g., "Write tally-odds so that the thing that does the tallying doesn't crash if the caller gives it someting other than a list of integers.") * Explain what the following code does. Turtles * Sketch the result of the following call * Write code to generate the following image (pictured or displayed) * Where will the turtle be and what direction will it be facing after these commands? Repetition with repeat and for each * Read this code and explain it * Perhaps incorporated into turtles, perhaps separate