Data
- Data definition:
TypeNameand values description - Interpretations: what the data shows
- Examples: define the representative set
- Template: function showing how to use this data
For example, temperature in celsius degrees:
;; A Celsius is a real number greater than -273.15
;; Interpretation: degree of temperature measured in
;; Celsius
(define CELSIUS-MIN -273.15)
(define CELSIUS-FREEZE 0)
(define CELSIUS-ROOM 20)
(define CELSIUS-BOIL 100)
(define CELSIUS-SUN 5600)
(define (celsius-temp c)
(... c ...))
If the data type has a defined set of values (for example, traffic light: red, green yellow), list all of them.
Functions
- Signature (
name : args -> ReturnType) - Purpose statement: very short, tweet length
- Tests:
(check-expect (name arg) value) - Code: start with an appropriate template from Data
Example: Class Standing and semester hours
;; Freshman: fewer than 32 SH
;; Sophomore: at least 32 SH but fewer than 64 SH
;; Junior: at least 64 SH but fewer than 96 SH
;; Senior: at least 96 SH
;; A SemesterHours is a non-negative integer
;; Interpretation: earned semester hours for an undergrad
;; at Northeastern University
(define SH-0 0)
(define SH-20 20)
(define SH-50 50)
(define SH-80 80)
(define SH-105 105)
(define (sh-template sh)
(... sh ...))
;; A ClassStanding is one of:
;; - "Freshman"
;; - "Sophomore"
;; - "Junior"
;; - "Senior"
;; Interpretation: class standing for an undergrad at
;; Northeastern University
(define CS-FR "Freshman")
(define CS-SO "Sophomore")
(define CS-JU "Junior")
(define CS-SE "Senior")
(define (cs-template cs)
(...
(cond
[(string=? cs CS-FR) ...]
[(string=? cs CS-SO) ...]
[(string=? cs CS-JU) ...]
[(string=? cs CS-SE) ...])))
class-standing function:
;; class-standing : SemesterHours -> ClassStanding
;; Computes the class standing given earned semester
;; hours
(check-expect (class-standing SH-0) CS-FR)
(check-expect (class-standing SH-20) CS-FR)
(check-expect (class-standing SH-50) CS-SO)
(check-expect (class-standing SH-80) CS-JU)
(check-expect (class-standing SH-105) CS-SE)
This function returns takes in SemesterHours as an input, so use the sh-template function as a starter template:
(define (class-standing sh)
(cond
[(< sh 32) CS-FR]
[(< sh 64) CS-SO]
[(< sh 96) CS-JU]
[else CS-SE]))
near-graduation? function
Design the function near-graduation? that determines if a student’s class standing is a senior.
;; near-graduation? : ClassStanding -> Boolean
;; Determines if the supplied class standing is a senior.
(check-expect (near-graduation? CS-FR) #false)
(check-expect (near-graduation? CS-SO) #false)
(check-expect (near-graduation? CS-JU) #false)
(check-expect (near-graduation? CS-SE) #true)
This function takes in the ClassStanding as a parameter, so use the cs-template function as a template:
(define (near-graduation? cs)
(cond
[(string=? cs CS-FR) #false]
[(string=? cs CS-SO) #false]
[(string=? cs CS-JU) #false]
[(string=? cs CS-SE) #true]))
Example: numerical data
Design a function liquid-water? that determines if the supplied temperature in Celsius is liquid (under ordinary conditions).
First, start with the function signature. We do not use Number for temperature because Celsius has a lower bound.
;; liquid-water? : Celsius -> Boolean
Since Celsius is a data type we defined, we need to follow the design recipe.
;; A Celsius is a real number greater than -273.15
;; Interpretation: degree of temperature measured in
;; Celsius
(define CELSIUS-MIN -273.15)
(define CELSIUS-FREEZE 0)
(define CELSIUS-ROOM 20)
(define CELSIUS-BOIL 100)
(define CELSIUS-SUN 5600)
(define (celsius-template c)
(... c ...))
Now, we can continue the design recipe for the functions:
;; liquid-water? : Celsius -> Boolean
;; Determines if water at the provided temperature in
;; celsius would be liquid
(check-expect (liquid-water? CELSIUS-MIN) #false)
(check-expect (liquid-water? CELSIUS-FREEZE) #false)
(check-expect (liquid-water? CELSIUS-MIN) #false)
(check-expect (liquid-water? CELSIUS-ROOM) #true)
(check-expect (liquid-water? CELSIUS-BOIL) #false)
(check-expect (liquid-water? CELSIUS-SUN) #false)
(define (liquid-water? c)
(and (> c CELSIUS-FREEZE)
(< c CELSIUS-BOIL)))
Example: itemizations
You are running a vehicle-rental shop that rents out bikes, scooters, and cars. Design a function num-spares to suggest the maximum number of wheels that could go on a particular vehicle.
;; A Vehicle is one of
;; - "bike"
;; - "scooter"
;; - "car"
;; Interpretation: a vehicle in our shop
(define VEHICLE-BIKE "bike")
(define VEHICLE-SCOOTER "scooter")
(define VEHICLE-CAR "car")
(define (vehicle-temp v)
(...
(cond
[(string=? v VEHICLE-BIKE) ...]
[(string=? v VEHICLE-SCOOTER) ...]
[(string=? v VEHICLE-CAR) ...])))
;; num-spares : Vehicle -> PosInteger
;; Returns the maximum number of wheels per the
;; given vehicle
(check-expect (num-spares VEHICLE-BIKE) 2)
(check-expect (num-spares VEHICLE-SCOOTER) 2)
(check-expect (num-spares VEHICLE-CAR) 4)
(define (num-spares v)
(cond
[(string=? v VEHICLE-BIKE) 2]
[(string=? v VEHICLE-SCOOTER) 2]
[(string=? v VEHICLE-CAR) 4]))