1. Structs
A friend is building a small operating system and they are currently working on how to manage files. Here is a data structure that they came up with
(define-struct file (name extension size))
;; 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
We are trying to develop a program to keep track of all equipment for a store that buys and sells computer accessories.
A popular category of accessories in the store is media (CDs, DVDs, etc.), and gaming peripherals (keyboards, mice, etc.).
Here are two data definitions for media and peripherals
;; A Media is one of
;; - 'dvd
;; - 'cd
;; - 'blueray
;; - 'floppy
;; INTERP: represents each kind of media kept at the store
;; A Peripheral is one of
;; - "keyboard"
;; - "mouse"
;; - "driving wheel"
;; - "headset"
;; - "controller"
;; INTERP: represents each kind of peripheral kept at the store
3. 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?))
;; 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?))
;; 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
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
4. 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 in
;; A College is one of
;; - "Computer Science"
;; - "Mathematics"
;; - "Electrical Engineering"
;; INTERP: represents the college the candidate is from
;; 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))
;; A Candidate is a (make-candidate Degree College Class Real)
;; INTERP: represents a candidate with their degree, college, class they are applying for a TAship, and their GPA
5. 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