Structure Lab

This lab explores the  definition of record-based structures. It is a short lab.




Image CC-BY jonasj @ Flickr.

Structure Problems

Before we tackle lists, we'll get used to simply building and and manipulating structured data. Remember to follow the Design Recipe for structures.

  1. Provide a datatype definition for a structure called a pit, representing points in time since midnight. A point in time consists of three numbers: hours, minutes, and seconds.

    Now develop the function time-diff. It consumes two pits, t1 and t2, and returns the number of seconds from t1 to t2. 

    For example:
    (time-diff (make-pit 1 2 3) (make-pit 4 5 6)) 

    should yield 10983. If you would like, I have written up a complete solution, which may give you a sense for what I think a full solution looks like.

  2. Provide a structure definition and a data definition for a game-score. A game-score is characterized by two pieces of information: a symbol representing the name of the team and a number representing the points-scored by that team.

  3. Develop a function that consumes two game-scores from a particular game and returns the name of the team that won or 'tie if the scores were equal. 

  4. Develop a datatype definition for a position in two-dimensional space called a posn. A posn should have an x and a y coordinate. 

  5. Provide a datatype definition for shapes. There are three kinds of shapes:
    1. A circle has a center (center isa posn?) and a radius (radius isa number?)
    2. A square has an upper-left corner (corner isa posn?) and a length (length isa number?)
    3. A rectangle has an upper-left corner (corner isa posn?), width (width isa number?), and height (height isa number?

    In each case, remember to provide a clear comment regarding the structure along with its definition.

  6. Develop the function area that consumes a shape and computes its area.

  7. Develop the function in-shape? that consumes a shape and a posn, and returns true if that posn is within the shape, false otherwise.


For each of these functions, I would like you to follow the design recipe and have small tests written before you write the functions themselves. You should write tests that you are confident exercise all of your code.

For example, if I was writing the function

(define (add a b) (+ a b))


then I would write, before writing the function:

;; CONTRACT
;; add : number number -> number
(= (add 3 5) 8)
(= (add 0 0) 0)


and so on. You should have already encountered the design recipe in your reading of HtDP. I'll mention it at the start of lab again.

Regarding Looping

Edsger W. Dijkstra

1930 - 2002

Edsger Dijkestra is a founder of our discipline. By "a founder," I mean he wrote some of the first languages, compilers, and algorithms we know and use daily.

One of his most cited pieces is the manuscript A Case against the GO-TO Statement. It was published by the ACM in 1968 under the title Go-to Statement Considered Harmful. Many of the citations simply reference the title, and not the content of the paper. If you wish to read more of his writings, you will find the Dijkestra Archive at UT Austin invaluable.




Consider Dijkestra's (original) manuscript in light of program and language design. Is his message still relevant to us in 2009? In what way? Why? Use your experience with languages like Java, Python, and occam-π, and consider his arguments in light of your experiences to date with a variety of programming languages. (2-3 pages)

Feedback

I had 2 goals in assigning this first laboratory.

  1. Provide you with the opportunity to practice defining and manipulating structured data.
  2. Get you thinking critically about looping structures in languages you have seen before in preparation for our exploration of Scheme's looping constructs.

If you would please rate this laboratory based on these goals, and provide whatever feedback you feel is necessary to improve this lab in the future, that would be excellent. 

Creative Commons License This work is licensed under a CC BY-SA 3.0 License.