### Pre-defined Data Definitions

Last updated: Sat, 11 Apr 2015 12:57:41 -0400

In this course, the following data definitions are pre-defined. In other words, you may use the data definition names and templates in your programs without first defining them yourself.

#### 1Scalar Data

Here are the scalar data definitions that you may use in this course.

However, since we are interested in representing information, you should almost always define an alias for these data definitions. See the data design section of the style guide for more guidelines.

 ; Any        ; represents all possible Racket values ; Number     ; represents any number, including imaginary and complex numbers ; Real ; NonNegReal ; excludes negative reals but includes zero ; PosReal    ; excludes zero ; Integer ; NonNegInt  ; excludes negative integers but includes zero ; Natural    ; same as NonNegInt ; PosInt     ; excludes zero ; Boolean ; Char ; String ; Symbol

#### 2Aliases

This section defines aliases for scalar data. You may use any of these in your programs without first defining them. However, you are responsible for satisfying any invariants.

A Coordinate represents an (x,y) position on a Universe (i.e., big-bang) canvas.

 ; A Coordinate is a Real. ; INTERP: ;  Represents either an x or y pixel coordinate on a Universe (big-bang) ;  canvas, where (0,0) is in the upper-left corner, and right and down are ;  the positive directions.

#### 3Image

; An Image is any value where applying image? from the 2htdp/image library to that value returns true
An Image is atomic data and thus may only be manipulated via function composition of functions from 2htdp/image .

#### 4Color

A Color represents a color recognized by the 2htdp/image library. A string c is a Color if (image-color? c) is true.

#### 5Posn

 ; A Posn is a (make-posn Coordinate Coordinate) ; INTERP: Represents an (x,y) coordinate on a Universe (big-bang) canvas. (define-struct posn (x y)) ; TEMPLATE: ; posn-fn : Posn -> ??? (define (posn-fn p) (... (posn-x p) ... (posn-y p) ...))

#### 6KeyEvent

 ; A KeyEvent is one of: ; - "a" ; - "b" ; - "c" ; - ... ; - " " ; space bar ; - "right" ; - "left" ; - ... ; INTERP: A KeyEvent represents a key-press in a Universe (big-bang) program.

 ; TEMPLATE: ; keyevent-fn : KeyEvent -> ??? (define (keyevent-fn kev) (cond [(key=? kev "a") ...] [(key=? kev "b") ...] [(key=? kev "c") ...] ...))

#### 7MouseEvent

 ; A MouseEvent is one of: ; - "button-up" ; - "button-down" ; - "drag" ; - "move" ; - ... ; INTERP: A MouseEvent represents a mouse click or movement in a ;  Universe (big-bang) program.

 ; TEMPLATE: ; mouseevent-fn : MouseEvent -> ??? (define (mouseevent-fn mev) (cond [(mouse=? mev "button-up") ...] [(mouse=? mev "button-down") ...] [(mouse=? mev "drag") ...] [(mouse=? mev "move") ...] ...))

#### 8ListOf<X>

 ; A ListOf is one of: ; - empty ; - (cons X ListOf)

(No interpretation is necessary because this data definition is parameterized over X, so we don’t know what information this data represents. However, the data definition for whatever X is better have an interpretation.)

 ; TEMPLATE: ; list-fn : ListOf -> ??? (define (list-fn lst) (cond [(empty? lst) ...] [else (... (X-fn (first lst)) ... (list-fn (rest lst)) ...)]))

#### 9NEListOf<X>

There are two alternative ways to define non-empty lists. The first refers to the ListOf definition while the second refers back to itself. Each may be more appropriate for different situations.

##### 9.1NEListOf<X>, alternative 1

; A NEListOf<X> is a (cons X ListOf<X>)
)

(No interpretation is necessary because this data definition is parameterized over X, so we don’t know what information this data represents. However, the data definition for whatever X is better have an interpretation.)

 ; TEMPLATE: ; nelist-fn : NEListOf -> ??? (define (nelist-fn nelst) (... (X-fn (first nelst)) ... (list-fn (rest nelst)) ...))

##### 9.2NEListOf<X>, alternative 2

 ; A NEListOf is one of: ; - (cons X empty) ; - (cons X NEListOf)

(No interpretation is necessary because this data definition is parameterized over X, so we don’t know what information this data represents. However, the data definition for whatever X is better have an interpretation.)

 ; TEMPLATE: ; nelist-fn : NEListOf -> ??? (define (nelist-fn nelst) (cond [(empty? (rest nelst)) (... (X-fn (first nelst)) ...)] [else (... (X-fn (first nelst)) ... (nelist-fn (rest lst)) ...)]))

#### 10SetOf<X>

 ; A SetOf is a ListOf ; WHERE: there are no duplicate elements, and ;        any ordering of the elements is considered the same set.

(No interpretation is necessary because this data definition is parameterized over X, so we don’t know what information this data represents. However, the data definition for whatever X is better have an interpretation.)

 ; TEMPLATE: ; set-fn : SetOf -> ??? (define (set-fn s) (cond [(empty? s) ...] [else (... (X-fn (first lst)) ... (set-fn (rest lst)) ...)]))

#### 11Maybe<X>

 ; A Maybe is one of: ; - false ; - X

(No interpretation is necessary because this data definition is parameterized over X, so we don’t know what information this data represents. However, the data definition for whatever X is better have an interpretation.)

 ; TEMPLATE: ; maybe-fn : Maybe -> ??? (define (maybe-fn val/#f) (cond [(false? val/#f) ...] [else (... val/#f ...)]))

#### 12Void

; A Void is a void.

If a value x is Void data, then (void? x) is #t.

Use this data definition as the return type for effectful functions and methods. See the Effectful Methods section of the style guide.