Guided Practice 2.1

The data definitions are:

;; A Size is one of
;; -- "small"
;; -- "medium"
;; -- "large"

;; size-fn : Size -> ??
(define (size-fn s)
  (cond
    [(string=? s "small") ...]
    [(string=? s "medium") ...]
    [(string=? s "large") ...]))

;; A Year is a PosInt[1900,2020]
;; interp: the year.  Must be between 1900 and 2020.

;; Year is Scalar data, so we don't need a template.

;; A Vineyard is ... /to be filled in/

(define-struct coffee (size type milk?))
(define-struct wine (vineyard year))
(define-struct tea (size type))

;; A BarOrder is one of
;; -- (make-coffee Size String Boolean)
;;  interp: 
;;   size is the size of cup desired
;;   type is the origin of the coffee (as a string)
;;   milk? tells whether milk is desired.
;; -- (make-wine Vineyard Year)
;;  interp:
;;   vineyard is the origin of the grapes
;;   year is the year of harvest
;; -- (make-tea Size String)
;;  interp: 
;;   size is the size of cup desired
;;   type is the type of tea (as a string)

;; bo-fn : BarOrder -> ??
;(define (bo-fn order)
;  (cond
;    [(coffee? order) (...
;                      (coffee-size order)
;                      (coffee-type order)
;                      (coffee-milk? order))]
;    [(wine? order) (...
;                    (wine-vineyard order)
;                    (wine-year order))]
;    [(tea? order) (...
;                   (tea-size order)
;                   (tea-type order))]))

The function definition was:

;; function4 : Size BarOrder -> Boolean
;; STRATEGY: structural decomposition on bo : BarOrder
(define (function4 s bo)
 (cond
   [(coffee? bo) (string=? (coffee-size bo) s)]
   [(wine? bo) false]
   [(tea? bo) (string=? (tea-size bo) s))])

Does this function definition follow the template?

YES, this definition follows the template. You don't need to use all of the fields. Also, it is ok to use a different variable name for the structured data.

Go on to the next lesson.


Last modified: Mon Aug 4 22:46:29 Eastern Daylight Time 2014