;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname lect04c) (read-case-sensitive #t) (teachpacks ((lib "universe.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "universe.ss" "teachpack" "2htdp"))))) ; A note on hw03, and how drawing images works: ; It's just like hw01's `donut`, which called `overlay` ; as a helper function. #| ; from the hw03 provided: ; (define (overlay/xy:nw bg x y pic) (place-image (put-pinhole pic 0 0) x y (put-pinhole bg 0 0))) ; an example: ; (define-struct flea (x y)) ; draw-flea : flea scene -> scene ; #;(define (draw-flea af as) (empty-scene 30 40)) ;stub ; A test case, for draw-flea: ; (check-expect (draw-flea (make-flea 1 3) (empty-scene 20 30)) #| Paste an image here! We created this expected-image by going to the; interactions window, calling 'circle' and 'overlay', and then cut/pasting it to here. |# (overlay/xy:nw (empty-scene 20 30) 1 3 (circle 8 'solid 'red))) ; N.B.: Test cases are *not* required for functions which return ; an image. But there's no fundamental reason why not; it's ; just a bit more difficult to generate the result by hand. ; But by doing so in the interactions window, notice how ; we decided on code: a flea will be a radius-8 red ; circle (whose nw corner is the struct's coordinates), ; it helps us figure out how to write our method: ; draw-flea : flea scene -> scene ; (define (draw-flea af as) (overlay/xy:nw as (flea-x af) (flea-y af) (circle 8 'solid 'red))) |# ; Review from yesterday: ; Data definition: ; a [list-of-num] is ; - empty, or ; - (make-cons [num] [list-of-num]) ;; Observe: this is like a grammar with ;; a recursive rule; the parts in brackets ;; are non-terminals (since we can't use color in this file). ;; That is, Cf: ;; [list-of-num] ::= empty | (cons [num] [list-of-num]) ; Examples of the data: empty (cons 903 empty) (define vic (cons 17 (cons 903 empty))) (cons -2 vic) ; Note/technicality: rather than say ; "(cons 17 (cons 903 empty)) returns a list", ; I'll often say things like ; "(cons 17 (cons 903 empty)) *is* a list. ; This makes it similar to other literals -- we don't ; point to the middle of a bunch of code at "17" and say ; 'the compiler is constructing (the bit pattern for) an int', ; we say it *is* an int. ; ; (Okay, that explanation is a bit specious, but close enough.) ;; Digression: ;; What if we didn't have 'cons' built in to scheme? ;; No problem, really it's just a struct with two fields: ;;; IMAGINARY (but motivating): we *could* write ;;; (define-struct kons (first-rest)) ;;; and then we'd just use: ;;; (define cons make-kons) ; constructor ;;; (define first kons-first) ; selector ;;; (define rest kons-rest) ; selector ;;; (define cons? kons?) ; predicate ;;; ;;; (That is, `cons`,`first` are the names built-in to ;;; scheme, but if they weren't we could just substitute ;;; `make-kons`, `kons-first` instead.) ; Recall the template we wrote at the end of last time: ;;; Template for any function which handles ;;; a list of numbers. ;;; ;; fun-for-lon : list-of-number -> number ;(define (fun-for-lon alon) ; (cond [(empty? alon) ...] ; [(cons? alon) ...(first alon) ; ...(fun-for-lon (rest alon))])) ; sum : list-of-number -> number ; (define (sum alon) (cond [(empty? alon) 0] [(cons? alon) (+ (first alon) (sum (rest alon)))])) (check-expect (sum empty) 0) (check-expect (sum (cons 903 empty)) 903) (check-expect (sum vic) 920) (check-expect (sum (cons -2 vic)) 918) ; length : list-of-number -> number ; (define (mylength alon) (cond [(empty? alon) 0] [(cons? alon) (+ 1 (mylength (rest alon)))])) (check-expect (mylength empty) 0) (check-expect (mylength (cons 903 empty)) 1) (check-expect (mylength vic) 2) (check-expect (mylength (cons -2 vic)) 3) ; squeach : list-of-number -> list-of-number ; Square each number in `alon`. ; (define (squeach alon) (cond [(empty? alon) empty] [(cons? alon) (cons (sqr (first alon)) (squeach (rest alon)))])) (check-expect (squeach empty) empty) (check-expect (squeach (cons 903 empty)) (cons (* 903 903) empty)) (check-expect (squeach vic) (cons 289 (cons (* 903 903) empty))) (check-expect (squeach (cons -2 vic)) (cons 4 (cons 289 (cons (* 903 903) empty)))) ;; contains? : list-of-number, number -> boolean ;; (define (contains? a-lon target) (cond [(empty? a-lon) false] [(cons? a-lon) (or (= target (first a-lon)) (contains? (rest a-lon) target))])) (check-expect (contains? empty 17) false) (check-expect (contains? (cons 3 empty) 17) false) (check-expect (contains? (cons 17 empty) 17) true) (check-expect (contains? vic 17) true) (check-expect (contains? vic 903) true) (check-expect (contains? vic 99) false) ; step through: (sum (cons 3 (cons 4 (cons 5 (cons 6 (cons 7 (cons 8 empty))))))) (squeach (cons 3 (cons 4 (cons 5 (cons 6 (cons 7 (cons 8 empty)))))))