; CMPU 101 ; Spring 2013 ; Lecture 10 (require 2htdp/image) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ANOTHER FUNCTION FOR CONSTRUCTING LISTS: ; ; The LIST function is easier to use and understand than is CONS ; repeated many times. The form is: (list element1 element2 ... ; elementN) and its output is a list (containing n elements in this ; case). ; ; SHORTHAND NOTATION FOR CONSTRUCTING LISTS: ; ; There is even a shorter notation for specifying lists, using ; a quote before the opening parenthesis: ; (list 1 3 5 4) is equal to '(1 3 5 4) ; ; This gets slightly tricky when using quoted symbols inside a ; list: ; (list 'a 'b 'd) is equal to '(a b d) ; ; meaning that every quoted symbol inside a quoted list is quoted ; as a result of the list being quoted. ; ; So writing '(a b c) is not the same as writing '('a 'b 'c). Try ; typing these lists in the interactions window to see the result. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; DESIGNING FUNCTIONS FOR A LIST OF STRINGS ;; ;; Data definition: ;; A list of strings (los) is either ;; 1. empty, or ;; 2. a (cons str ls), where str is a string and ls is a los ;; with one less string than (cons str ls). ;; ;; Example los's: ;(define GRADES ; (cons ; "A" ; (cons ; "A-" ; (cons ; "B+" ; (cons ; "B" ; (cons ; "B-" ; (cons ; "C+" ; (cons ; "C" ; (cons ; "C-" ; (cons ; "D" ; (cons "F" empty))))))))))) ;; THE DEFINITION ABOVE IS WRITTEN IN A SHORTER FORM BELOW: (define GRADES '("A" "A-" "B+" "B" "B-" "C+" "C" "C-" "D" "F")) ;(define WORDS ; (cons "hello" ; (cons "there" ; (cons "how" ; (cons "are" ; (cons "you" ; (cons "today" empty))))))) ;; THE DEFINITION ABOVE IS WRITTEN IN A SHORTER FORM BELOW: (define WORDS '("hello" "there" "how" "are" "you" "today")) ;(define ANIMALS ; (cons ; "monkey" ; (cons ; "cat" ; (cons ; "gerbil" ; (cons ; "moose" ; (cons ; "octopus" ; (cons "eagle" empty))))))) (define ANIMALS '("monkey" "cat" "gerbil" "moose" "octopus" "eagle")) ;; In-class example 1: ;; Write a function that consumes a los and produces ;; the length of the maximum length string in the los. ;; Contract: (max-length los) -> number ;; Header: (define max-length (lambda (a-los) ... )) ;; Purpose: produce the length of the longest string in a-los ;; We can test our function on the los's we defined above. (check-expect (max-length GRADES) 2) (check-expect (max-length ANIMALS) 7) (check-expect (max-length WORDS) 5) (check-expect (max-length empty) 0) ;; the empty case should always be checked (define max-length (λ (a-los) (cond ;; base case; an empty list has no strings of length > 0 [(empty? a-los) 0] ;; recursive case; find the largest length string using max function [else (max (string-length (first a-los))(max-length (rest a-los)))]))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; In-class example 2: Writing a function that consumes a los and ;; produces a los ;; Write a function that consumes a los, a-los, and a number, x, and produces ;; a list of the strings of length x. ;; Contract: (strings-of-length-x los number) -> los ;; Header: (define strings-of-length-x (lambda (a-los num) ...)) ;; Purpose: produce a list of all strings in a-los of length equal to num ;; Pre-Function Tests: (check-expect (strings-of-length-x GRADES 2) '("A-" "B+" "B-" "C+" "C-")) (check-expect (strings-of-length-x ANIMALS 6) '("monkey" "gerbil")) (check-expect (strings-of-length-x WORDS 4) '()) (check-expect (strings-of-length-x empty 8) empty) (check-expect (strings-of-length-x '("me" "you" "cat") 3) '("you" "cat")) ;; the empty case is always checked (define strings-of-length-x (λ (a-los num) (cond ;; base case; the empty list contains no strings of length > 0 [(empty? a-los) empty] ;; recursive case 1; the string length of first equals num [(= num (string-length (first a-los))) (cons (first a-los) (strings-of-length-x (rest a-los) num))] ;; recursive case 2: the string length of first not equal to num [else (strings-of-length-x (rest a-los) num)]))) ;; NOTE THAT THIS SOLUTION HAS TWO RECURSIVE CASES, ONE IN WHICH ;; THE STRING-LENGTH OF THE FIRST ELEMENT IS EQUAL TO X, AND ONE ;; IN WHICH THE STRING-LENGTH OF THE FIRST ELEMENT IS NOT EQUAL ;; TO X. A RECURSIVE CALL MUST BE MADE IN EITHER CASE. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; DESIGNING FUNCTIONS FOR LISTS OF IMAGES ;; ;; The definition of a list of rectangle images looks just like ;; the definition for a list of numbers (and most list definitions ;; look like any other list definition, at least in the form of the ;; definition). ;; Data definition for list of rectangles: ;; A list of rectangles (lor) is either ;; 1. empty, or ;; 2. a (cons r lr), where r is a rectangle and lr is an lor ; ; When working with images, DrRacket offers a variety of options for ; displaying multiple images. The most frequently used option for ; displaying multiple images is the empty-scene function. The contract ; for this function is: (empty-scene width height) -> image (NOTE: a scene ; is of type image). ; ; An empty-scene function creates an outline of a black rectangle of ; the given width and height with a white interior. Since this function ; is used frequently in programs that work with images, it is common to ; define a constant to hold the empty scene image (see the constant called ; MT below). ; ; GO OVER LAYOUT OF X/Y PLANE IN COMPUTER GRAPHICS. ; ; ; Placing images on a scene: ; ; There are also several options for drawing images on a scene. The one we ; will use most often is the function place-image, with the following con- ; tract: ; Contract: (place-image image number number scene) -> image ; Header: (define place-image (lambda (im x y scn) ..)) ; Purpose: To position im on scn such that the pinhole of im ; is at coordinates (x, y) on scn ; ; ;; Example rect objects (define RECT1 (rectangle 150 150 "solid" "red")) (define RECT2 (rectangle 360 100 "outline" "blue")) (define RECT3 (rectangle 60 50 "solid" "green")) (define RECT4 (rectangle 220 122 "outline" "black")) (define RECT5 (rectangle 100 460 "solid" "magenta")) ;; Example lor's: (define LOR1 (list RECT1 RECT2 RECT3 RECT4 RECT5)) (define LOR2 (list RECT5 RECT4 RECT3 RECT2 RECT1)) ;; Constants to be used in function ; ; (define WIDTH 400) ; (define HEIGHT 400) ; (define CENTER-W ?) ; (define CENTER-H ?) ; (define MT (empty-scene WIDTH HEIGHT)); base case of a scene ; ;; In-class Example 3: Write a function that consumes a ;; list of rectangles and overlays each rectangle on the ;; list into an empty scene ;; Contract: (draw-rect-scene lor) -> image ;; Header: (define draw-rect-scene (lambda (alor) ... )) ;; Purpose: to center each rect in alor onto the center of ;; an empty scene ;; Function: ;; visual tests ; (printf "(draw-rect-scene LOR1) => ~a~%" (draw-rect-scene LOR1)) ; (printf "(draw-rect-scene LOR2) => ~a~%" (draw-rect-scene LOR2)) ; (printf "(draw-rect-scene LOR3) => ~a~%" (draw-rect-scene LOR3))