CSC151 2010S, Class 17: Exam 1 Discussion Overview: * General issues. * What is functional programming? * Issues with particular problems. * Additional Q&A * Lab (if time). Admin: * Reading for tomorrow: Turtle Graphics. * EC for Wednesday's panel on Open Information (4:30 pm Burling). * EC for Thursday's convo or Wednesday night's talk (where science and religion overlap) * EC for Thursday's CS Extra (4:15 3821). * EC for Friday's CS Table (noon, JRC PDR). * Reminder: Sleep/Rest, Eat well, Wash hands * Are there questions on Assignment 4? General exam comments * Test taking strategies * Look at the whole exam first * Don't take the exam all at once * Ask Sam for help! * Start early What is functional programming? * Computation through the evaluation of expressions * Amazingly, this happens in almost every programming language * Computation that avoids mutable data * mutable == changing/changeable * Our drawings are an instance of immutable data * Can be counter-intuitive * Numbers are an instance of immutable data * Once we've named a value, the name retains the same value * But we've seen mutable data: images are mutable * The same operation will behave differently at different states of the program (image-get-pixel image 2 3) * Procedures are functions in the mathematical sense * It provides an output for an input * It does math! * "The mathematical sense" => Given the same inputs, a function always provides the same output * Functional languages generally don't give you access to "state" * "state" is something that is changeable * In Scheme, we do sometimes change the state with the ! procedures * The state of an image is the setting of each pixel of the image * The state of a named value is the value associated with the name * You can change part of a state (e.g., one pixel) * The state of the computer is the setting of every memory location (plus a bit more) * Scheme is NOT a pure functional language * Citations of Web pages * Author * Title * Date of access * ... (define simple-circle (drawing-scale (drawing-shift drawing-unit-circle 0.5 0.5) 50)) (define drawing-render (lambda (drawing) (image-show (drawing->image drawing (inexact->exact (round (drawing-right drawing))) (inexact->exact (round (drawing-bottom drawing))))))) (define drawing-hpair (lambda (drawing1 drawing2) (drawing-group drawing1 (drawing-hshift drawing2 (drawing-right drawing1))))) (define drawing-vpair (lambda (drawing1 drawing2) (drawing-group drawing1 (drawing-vshift drawing2 (drawing-bottom drawing1))))) (define warhol (lambda (drawing) (drawing-vpair (drawing-hpair (drawing-recolor drawing "red") (drawing-recolor drawing "green")) (drawing-hpair (drawing-recolor drawing "blue") (drawing-recolor drawing "yellow"))))) (define drawing-background (lambda (drawing color border) (drawing-group (drawing-vscale (drawing-hscale (drawing-recolor (drawing-shift drawing-unit-square 0.5 0.5) color) (+ border border (drawing-width drawing))) (+ border border (drawing-height drawing))) (drawing-shift drawing border border)))) (define andy (lambda (drawing) (drawing-vpair (drawing-hpair (drawing-background drawing "red" 10) (drawing-background drawing "blue" 10)) (drawing-hpair (drawing-background drawing "green" 10) (drawing-background drawing "yellow" 10)))))