1. Structs

A friend is building a small operating system and they are currently working on how to manage files. Here are some data definitions that they came up with

;; A FileName is a String
;; A FileExtension is a Symbol
;; A FileSize is a NonNegInteger
Practise Exercises
  • 1) Design a new data definition with the name file. Your data definition must capture the properties of file. A file must have a file name, a file extension and a file size.

  • 2) Write down the names of all the autogenerated functions defined with the creation of structure file.

  • 3) Write down the signatures of each function defined with the creation of the structure file.

  • 4) Write down examples of files using the file structure.

  • 5) Write a deconstructor template for file.

We would now like to develop a function that given a file will print out the file’s information. We would like however to pretty print information so that we can present it to the user.

The format for printing out file information is as follows

  1. The name of the file

  2. A full stop .

  3. The extension of the file

  4. 4 spaces

  5. The size of the file

For example (define file1 (make-file "thesis" 'doc 1234)) should return the following formatted string

"thesis.doc    1234"
Practise Exercises
  • 6) Design a function called change-extension that given a file f and a new extension ext, returns a new file that has the same information as f except that its extension is now ext.

  • 7) Design a function that given a file will return a string with the file’s information using the desired format.

2. Itemizations that use structures

We are trying to build a graphics library that supports basic shapes. The first iteration of our library has the following data definition

;; An Outline is a Boolean
;; INTERP: true means use outline for the shape, false means use solid

(define-struct cir (radius color outline))
;; Constructor Template:
;; A Circle is (make-cir NonNegInteger Color Outline)
;; INTERP: represents a circle with its radius, color and if it has an outline


(define-struct squ (side color outline))
;; Constructor Template:
;; A Square is (make-squ NonNegInteger Color Outline)
;; INTERP: represents a square with its side, color and if it has an outline


;; A Shape is one of
;; - Circle
;; - Square
Practise Exercises
  • 8) The data definitions provided are incomplete. You are asked to complete these data definitions.

  • 9) Design a function that given a Shape returns the area of the Shape. You might want to use DrRacket’s check-within in order to test inexact numbers.

We would now like to add the following new shapes to our library

  • A rectangle with a width, height, color and outline

Practise Exercises
  • 10) Add rectangle to Shape 's data definition.

  • 11) Update all affected templates.

  • 12) Update your function that given a Shape returns the area of the Shape to accommodate your additions.

3. Nested structures

We are writing a prototype program that creates and manipulates boarding passes at airports. Our team came up with the following incomplete data definitions

(define-struct traveller (first last))
;; A Traveller is (make-traveller String String)
;; INTERP: represents the first and last name of the traveller

;; A City is a String
;; INTERP: represents the name of a City


;; A 1String is a String that contains 1 character, e.g., "A"


(define-struct gate (prefix suffix))
;; A Gate is (make-gate 1String PosInt)
;; INTERP: represents the gate number as a one letter string and an integer.


;; Hours is a PosInt
;; WHERE : 0 <= hours <= 23
;; INTERP: represents hours on a 24-hour clock

;; Minutes is a PosInt
;; WHERE: 0 <= minutes <= 59
;; INTERP: represents minutes on a clock

(define-struct time (hours minutes))
;; A Time is (make-time Hours Minutes)
;; INTERP: represents time as hours and minutes

(define-struct boarding-pass (traveller source-city destination-city gate time))
;; A BoardingPass is (make-boarding-pass Traveller City City Gate Time)
;; WHERE: destination-city differs from source-city
;; INTERP: represents a boarding pass with the traveller's name, the source and destination
;;         city the gate and the time of departure
Practise Exercises
  • 13) Complete the data definitions.

  • 14) Design a pretty print function that given a boarding pass returns a formatted string. Here is a sample.

John Doe, Seattle -> Boston, Gate A34 @ 22:30
  • 15) Design a function that given two boarding passes returns true if the two boarding passes are for the same traveller, same gate and same time, else returns false. You are not allowed to use equal? eqv? or eq?