(define title "Using Abstractions") (define description "") ;;; Admin ;;; - TA meeting ;;; + Snake & tetris great assignments ;;; Stressed coding skills, students exhibit classic probs ;;; + don't do examples first ;;; + no templates ;;; + don't do unit testing - big bang debugging ;;; Writing test cases after. ;;; + I.e.: not following design recipes ;;; + Examples don't have to be complex! ;;; Simple examples great! They write your code for you. ;;; Handle simple cases, complex takes care of itself. ;;; + code too big -- break into pieces ;;; Easier to think about; *much* easier to debug. ;;; + partner-slacking shows up. We have mechanisms... ;;; SERPENTINE ;; ------------------------------------------------------ ;; PROBLEM: ;; add10-to-all: [Listof Number] -> [Listof Number] ;; add 10 to each number on the list ;; ------------------------------------------------------ ;; given: (list 1 2 3) ;; wanted (list 11 12 13) ;; --> test (define (add10-to-all l) ;; (map f (list x y z)) is (list (f x) (f y) (f z)) ;; use it and what should f do? (map add10 l)) ;; Number -> Number ;; add 10 to the number (define (add10 x) (+ x 10)) (equal? (add10-to-all '(1 2 3)) '(11 12 13)) ;; why are the contracts working out? ;; map : [X -> Y] [Listof X] -> [Listof Y] ;; X = Number ;; Y = Number ;; ------------------------------------------------------ ;; use LOCAL for the ugly function definition ;; [Listof Number] -> [Listof Number] ;; add 10 to each number on the list ;; given: (list 1 2 3) ;; wanted (list 11 12 13) ;; --> test (define (add10-to-all.v2 l) (local ((define (add10 x) (+ x 10))) (map add10 l))) (equal? (add10-to-all.v2 '(1 2 3)) '(11 12 13)) ;; ------------------------------------------------------ ;; PROBLEM: ;; add-x-to-all: [Listof Number] Number -> [Listof Number] ;; add the given number (x) to each item on the list ;; ------------------------------------------------------ ;; given: (list 1 2 3) 5 ;; wanted (list 6 7 8) (define (add-x-to-all l x) (local ((define (add10 i) (+ i x))) (map add10 l))) (equal? (add-x-to-all '(1 2 3) 5) '(6 7 8)) ;; ------------------------------------------------------ ;; PROBLEM: ;; unit? : [Listof Number] -> Boolean ;; are all numbers on the list between -1 and +1 (incl.)? ;; ------------------------------------------------------ ;; given: (list -1 0 +.5 -.3 +1) ;; wanted true (define (unit? l) (local ((define (between-1+1 x) (<= -1 x +1))) (andmap between-1+1 l))) (equal? (unit? (list -1 0 +.5 -.3 +1)) true) (equal? (unit? (list -1 0 +.5 +1.1)) false) ;; andmap : [X -> Boolean] [Listof X] -> Boolean ;; what is X here? X = Number ;; ------------------------------------------------------ ;; PROBLEM: ;; in-range? : [Listof Number] Number Number -> Boolean ;; are all numbers on the list between L and R (incl)? ;; ------------------------------------------------------ (define (in-range? lst L R) (local ((define (between-1+1 x) (<= L x R))) (andmap between-1+1 lst))) (equal? (in-range? (list -1 0 +.5 -.3 +1) -1 +1) true) (equal? (in-range? (list -1 0 +.5 +1.1) -1 +1) false) ;; WHAT IS A LOOP? ;; Perform some computation on every element of a collection ;; then combine the results together somehow. ;; Collection: num range, list... but also tree. ;; Why are loops so important? ;; - programs small; data big. ;; - theoretical source of power ;; How many loops in C or Java? 4? 5? ;; How many loops in Scheme? Many as you like. ;; What if I need some "loop" pattern not in std collection? ;; Write my own loop! In Scheme, can pass computation around. ------- ;;; Now teach lambda, ;;; then go back and redo examples with lambda. ------- ;; ------------------------------------------------------ ;; PROBLEM ;; paint : [Listof Posn] -> Image ;; paint red color spots at the given posn-s ;; on 100 x 100 canvas ;; ------------------------------------------------------ (define (paint l) (local ((define (one-dot p i) (place-image dot (posn-x p) (posn-y p) i)) (define dot (circle 3 'solid 'red))) (foldr one-dot (empty-scene 100 100) l))) (equal? (paint (list (make-posn 20 20))) (place-image (circle 3 'solid 'red) 20 20 (empty-scene 100 100))) ;; foldr : [X Y -> Y] Y [Listof X] -> Y ;; what are X and Y? X = Posn, Y = Image