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
Your friend would also like to capture programs on their operating system.
They would like to capture the following information about programs
-
The program’s name
-
The program’s version number
-
The file extension that this program uses when creating and saving files
-
The size (amount of storage occupied) by this program.
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
-
The name of the file
-
A full stop
.
-
The extension of the file
-
4 spaces
-
The size of the file
For example (define file1 (make-file "thesis" 'doc 1234))
should return the following formatted string "thesis.doc 1234"
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
-
The name of the program
-
4 spaces
-
The program’s version
-
4 spaces
-
The file extension that this program is responsible for
-
4 spaces
-
The size of the program
For our new operating system we would also like the ability to detect which program is responsible for which files.
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
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
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
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