On this page:
1 Scalar Data
2 Aliases
3 Image
4 Color
5 Posn
6 Key  Event
7 Mouse  Event
8 List  Of<X>
9 NEList  Of<X>
9.1 NEList  Of<X>, alternative 1
9.2 NEList  Of<X>, alternative 2
10 Set  Of<X>
11 Maybe<X>
12 Void

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.

1 Scalar 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

2 Aliases

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.

3 Image

; 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 .

4 Color

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

5 Posn

; 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) ...))

6 KeyEvent

; 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") ...]
    ...))

7 MouseEvent

; 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") ...]
    ...))

8 ListOf<X>

; A ListOf<X> is one of:
; - empty
; - (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:
; list-fn : ListOf<X> -> ???
(define (list-fn lst)
  (cond
    [(empty? lst) ...]
    [else (... (X-fn (first lst)) ...
               (list-fn (rest lst)) ...)]))

9 NEListOf<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.1 NEListOf<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<X> -> ???
(define (nelist-fn nelst)
  (... (X-fn (first nelst)) ...
       (list-fn (rest nelst)) ...))

9.2 NEListOf<X>, alternative 2

; A NEListOf<X> is one of:
; - (cons X empty)
; - (cons X NEListOf<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<X> -> ???
(define (nelist-fn nelst)
  (cond
    [(empty? (rest nelst)) (... (X-fn (first nelst)) ...)]
    [else
     (... (X-fn (first nelst)) ...
          (nelist-fn (rest lst)) ...)]))

10 SetOf<X>

; A SetOf<X> is a ListOf<X>
; 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<X> -> ???
(define (set-fn s)
  (cond
    [(empty? s) ...]
    [else (... (X-fn (first lst)) ...
               (set-fn (rest lst)) ...)]))

11 Maybe<X>

; A Maybe<X> 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<X> -> ???
(define (maybe-fn val/#f)
  (cond
    [(false? val/#f) ...]
    [else (... val/#f ...)]))

12 Void

; 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.