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
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"
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
We would now like to add the following new shapes to our library
-
A rectangle with a width, height, color and outline
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