Your template contains a list of temperatures, temp-list. Each tempearture is a pair of the form:
(tagged-temperature . temp-category)
where tagged-temperature is a temperature in one of the three representations given above.
Write a procedure (closest-temp-category temp temp-list) that, given a temperature in either Fahrenheit, Celsius, or Kelvin representation, returns the temperature category of the temperature in temp-list that is closest to temperature. If two or more temperatures are the same distance, choose the one that occurs first in temp-list.
Test a number of cases, including those provided in the template.
When it's zero, it's freezing!
when it's 10, it's not.
when it's 20, it's warm.
when it's 30, it's hot!
(Common memory aid for Celsius)
We're given these functions to work with
(define (attach-tag type-tag contents)
(cons type-tag contents))
(define (type-tag datum)
(if (pair? datum)
(car datum)
(error "Bad typed datum -- TYPE")))
(define (contents datum)
(if (pair? datum)
(cdr datum)
(error "Bad typed datum -- CONTENTS")))
; Two-Dimensional Table code Modified from Text Section 3.3
(define (2d-get key-1 key-2 table)
(let ((subtable (assoc key-1 (cdr table))))
(if subtable
(let ((record (assoc key-2 (cdr subtable))))
(if record
(cdr record)
()))
())))
(define (2d-put! key-1 key-2 value table)
(let ((subtable (assoc key-1 (cdr table))))
(if subtable
(let ((record (assoc key-2 (cdr subtable))))
(if record
(set-cdr! record value)
(set-cdr! subtable
(cons (cons key-2 value)
(cdr subtable)))))
(set-cdr! table
(cons (list key-1
(cons key-2 value))
(cdr table)))))
'ok)
(define (make-table)
(list '*table*))
; Generic operator analagous to more general apply-generic from page 184
; in the text. Uses a specified table to be compatible with 2dtable.scm
; for systems that do not include tables by default
(define (operate op obj t)
(let ((proc (2d-get op (type-tag obj) t)))
(if (not (null? proc))
(proc (contents obj))
(error "undefined operator for this type")
)
)
)
;; Main Table for Data Directed Programming
(define T (make-table))
;;;;
;;;; Step 4 - Hot, Cool, Cold?
;;;;
(define (closest-temp-category temp temp-list)
"your code here"
)
(display "=== TEST CASES [STEP 4] ===") (newline)
(define temp-list '(((celsius . 0) . freezing)
((celsius . 10) . cool)
((celsius . 20) . warm)
((celsius . 30) . hot)))
(define t1 (make-fahrenheit-from-fahrenheit 100))
(define t2 (make-celsius-from-fahrenheit 30))
(define t3 (make-kelvin-from-celsius 15))
(define t4 (make-fahrenheit-from-kelvin 290))
(define t5 (make-celsius-from-kelvin 320))
(define test-cases-step-4
'(
(closest-temp-category t1 temp-list) ; hot
(closest-temp-category t2 temp-list) ; freezing
(closest-temp-category t3 temp-list) ; cool
(closest-temp-category t4 temp-list) ; warm
(closest-temp-category t5 temp-list) ; hot
))
(do-tests 4)
The idea is that the temp-list is a list of pairs in which the first of each pair is another pair. Each of the data points should be able to be changed and the code should still work. We have also written code for each of the different temperature conversions. The test cases and do-tests is another function designed to automatically run predefined test cases on our function to ensure it works properly.
This probably is kind of confusing so if anyone thinks they can help me but would like a little clarification please ask. Thanks in advance.
edit: And for more clarification, I really just have no idea where to start. I don't even know if it's possible to do this recursively or iteratively.. :/


Sign In
Create Account

Back to top









