CSC 270 - Survey of Programming Languages: Scheme (Racket)

Compound Data I: Structures

Compound Data

Positions of Pixels

Example - Calculating distance from the origin

Defining a structure's value

Defining the square of x

Exercise

  • Dr Scheme provides the following graphics operations:
  • Evaluate the following expressions:
  • Structure Definitions

    Defining a Structure

    Defining a Structure - An Example

    Example - we manage several rock stars and we want to keep track of our data about them. We could write:

    (define-struct star (last first instrument sales))

    Consuming and Producing Structures

    Data Definitions

    Designing Programs for Compound Data

    Data Analysis and Design

    Writing the Contract

    Writing the Template

    Writing the Body

    Revised Design Recipe for Compound Data

    PhaseGoalActivity
    Data Analysis & DesignTo formulate a group of data definitions Determine how many pieces of data describe important parts of the problem - then add a structure definition and a data definition
    Contract Header & PurposeSpecify program name, inputs & outputs, and purpose, and write a program headerName the program, inputs, outputs, and specify its purpose.
    Examples Characterize the input/output relationship via examples Search the program for examples, validate these examples.
    Template Formulate an outline for a groups of programs Where parameters stand for compounds values, place selector expressions in the program. If the program is conditional, place selectors in each branch.
    Body To define the programDevelop a Scheme expression that uses Scheme's primitive operation, other programs selector expressions and variables
    Test To expose mistakes Apply the program to the inputs of the examples, check that the outputs are as predicted.

    An example of the Design Recipe

    [Back to the Notes Index]