1. Structs

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

(define-struct file (name extension size))
;; Constructor Template:
;; A File is (make-file String Symbol NonNegInteger)
;; INTERP: represents a file with the file name, the file extension
;;         and the file size
Practise Exercises
  • 1) Write down the names of all autogenerated functions defined with the creation of structure file.

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

  • 3) Write down a purpose statement for each autogenerated function defined due to the struct file.

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

  • 5) Write a deconstructor template for file.

Your friend would also like to capture programs on their operating system.

They would like to capture the following information about programs

  1. The program’s name

  2. The program’s version number

  3. The file extension that this program uses when creating and saving files

  4. The size (amount of storage occupied) by this program.

Practise Exercises
  • 6) Design a structure that captures the information of a program in the operating system.

  • 7) Write down the names of all the autogenerated functions defined due to your new structure.

  • 8) Write down the signatures of each autogenerated function defined due to your new structure.

  • 9) Write down a purpose statement for each autogenerated function defined due to your new structure.

  • 10) Write down examples of programs using your new structure.

  • 11) Write a deconstructor template for your new structure.

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
  • 12) Design a function that given a file will return a string with the file’s information using the desired format.

  • 13) Design a function 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.

    • 14) Design a function that given a file f and a size s, returns a new file that has the same information as f except that its size is now increased by s.

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

The format for printing out the program’s information is as follows

  1. The name of the program

  2. 4 spaces

  3. The program’s version

  4. 4 spaces

  5. The file extension that this program is responsible for

  6. 4 spaces

  7. The size of the program

Practise Exercises
  • 15) Design a function that given a program will return a string with the program’s information using the desired format.

  • 16) Design a function that given a program p, a version v, and, a size s returns the same information as p except that its version is now v and its size is now s.

For our new operating system we would also like the ability to detect which program is responsible for which files.

Practise Exercises
  • 17) Design a function that given a file and a program already installed in the operating system returns true if the program is responsible for this file, i.e., the program’s extension matches the file’s extension.

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 incomplete 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
;; INTERP: represents the known list of shapes that we can draw
Practise Exercises
  • 18) Write a deconstructor templates for these data definitions.

  • 19) Design a function that given a Shape returns an appropriate Image.

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

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

  • An equilateral triangle with the length of the side of the triangle, color and outline

Practise Exercises
  • 20) Add rectangle and equilateral triangle to Shape 's data definition.

  • 21) Update all affected elements of your data definition (deconstructor templates, examples etc)

  • 22) Update your function that given a Shape returns the appropriate Image.

  • 23) Write a new function that given a Shape returns the area of the Shape.

3. Structures that use itemizations

The department is building a program to deal with TA applications. Here are some data definitions they already have developed

;; A Degree is one of
;; - 'MS
;; - 'BSc
;; - 'PhD
;;INTERP: represents the degree the candidate is pursuing

;; A College is one of
;; - "Computer Science"
;; - "Mathematics"
;; - "Electrical Engineering"
;; INTERP: represents the college the candidate is member of


;; A Class is one of
;; - 'CS101
;; - 'CS201
;; - 'CS301
;; - 'CS401
;; INTERP: represents the course numbers for which the candidate can apply


(define-struct candidate (degree college class gpa))
;; Constructor Template:
;; A Candidate is a (make-candidate Degree College Class NonNegReal)
;; INTERP: represents a candidate with their degree, college, class they are applying for a TAship, and their GPA
Practise Exercises
  • 24) Complete the preceding data definitions.

  • 25) Design a function that given a candidate and a course number returns true if the candidate applied for this course number and their GPA is greater or equal to 3.5.

4. 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))
;; Constructor Template:
;; 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))
;; Constructor Template:
;; A Gate is (make-gate 1String PosInt)
;; INTERP: represents the gate number as a one letter string and an integer.


;; Hours is a PosInt within the range [0,23]
;; INTERP: represents hours on a 24-hour clock

;; Minutes is a PosInt within the range [0,59]
;; INTERP: represents minutes on a clock

(define-struct time (hours minutes))
;; Constructor Template:
;; 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))
;; Constructor Template:
;; A BoardingPass is (make-boarding-pass Traveller City City Gate Time)
;; INTERP: represents a boarding pass with the traveller's name, the source and destination
;;         city the gate and the time of departure
Practise Exercises
  • 26) Complete the data definitions.

  • 27) 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
  • 28) Design a function that given two boarding passes returns true if the two boarding passes are for the same traveller, same source and destination city, same gate and same time, else returns false. You are not allowed to use equal? eqv? or eq?