(define title "LOCAL") (define description "") ;; reminder: two weeks to go to the exam (place-image (text "14 days to go" 44 'red) 30 50 (empty-scene 500 500)) ;; wouldn't it be nice if we centered this ;; we need to compute the height and width ;; of the text to get this right --------- (define txt (text "14 days to go" 44 'red)) (define sce (empty-scene 500 500)) (define lft (quotient (- 500 (image-width txt)) 2)) (define top (quotient (- 500 (image-height txt)) 2)) (place-image txt lft top sce) ;; now let's make all of this independent of ;; the specific str and scene -------------- ;; specifics (define str "14 days to go") (define scn (empty-scene 500 500)) ;; general (define width (image-width scn)) (define height (image-height scn)) (define tex (text str 44 'red)) (define lef (quotient (- width (image-width tex)) 2)) (define tpp (quotient (- height (image-height tex)) 2)) (place-image tex lef tpp scn) ;; now let's turn this into a function ------- ;; Scene String -> Scene ;; add the given string to the center of the scene (define (add-greeting scn str) (place-image (text str 44 'red) (quotient (- (image-width scn) (image-width (text str 44 'red))) 2) (quotient (- (image-height scn) (image-height (text str 44 'red))) 2) scn)) ;; TESTS ;(equal? (add-greeting scn str) (place-image tex lef tpp scn)) ;; oh how painful. Why can't we just make these calculations ;; inside of the function? --------------------------------- ;; YES: graduated! Use Intermediate now. #; (local ((define ... ...) (define ... ...) (define ... ...)) ...) ;; works. Watch! (define (+greeting scn str) (local ((define width (image-width scn)) (define height (image-height scn)) (define tex (text str 44 'red)) (define lef (quotient (- width (image-width tex)) 2)) (define tpp (quotient (- height (image-height tex)) 2))) (place-image tex lef tpp scn))) ;(equal? (+greeting scn str) (place-image tex lef tpp scn)) ;; don't use check syntax! it's broken on intermediate. bug rpt filed. ;; STEP and demonstrate ;;; Quiz ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; AExp is one of ;;; - 'x ;;; - Number ;;; - (make-unary AExp) ;;; - (make-add AExp AExp) ;;; (define-struct unary (exp)) ;;; (define-struct add (left right)) ;;; subst: AExp Number -> AExp ;;; replace all occurrences of 'x in a with n ;;; (define (subst a n) ...) ;;; 10 minutes to solve ;; --------------------------------------------------------------- ;; NEL (non-empty list of numbers) is one of: ;; -- (cons Number empty) ;; -- (cons Number NEL) ;; largest : NEL -> Number ;; determine the largest number in the list (define (largest lon) (cond [(empty? (rest lon)) (first lon)] [else (cond [(> (largest (rest lon)) (first lon)) (largest (rest lon))] [else (first lon)])])) (equal? (largest (list 6 1 2 9 0)) 9) ;; (largest (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30)) (time (largest (list 1 2 3 4 5 6 7 8 9 10))) (time (largest (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20))) (define (maximum lon) (cond [(empty? (rest lon)) (first lon)] [else (local ((define fst (first lon)) (define max (maximum (rest lon)))) (cond [(> max (first lon)) max] [else (first lon)]))])) (equal? (maximum (list 6 1 2 9 0)) 9) ;(maximum (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30)) (time (maximum (list 1 2 3 4 5 6 7 8 9 10))) (time (maximum (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20))) ;; WHY? STEP (largest (list 1 2 3)) (maximum (list 1 2 3))