#lang racket
(define (fahrenheit->celsius f)
(/ (- f 32) 1.8))
(define (celsius->fahrenheit c)
(+ 32 (* c 1.8)))
;; return a list (fahrenheit celsius)
(define (convert-to-temperature-pairs n scale)
(cond ((eq? scale 'fahrenheit) (list n (inexact->exact (round (fahrenheit->celsius n)))))
((eq? scale 'celsius) (list (inexact->exact (round (celsius->fahrenheit n))) n))
(else '())))
(define (create-table-in-range min max step scale)
(letrec ((create-table-aux (lambda (current step tail)
(if (>= 0 step) ;; guard for negative or zero increments
'()
(if (> current max) ;; check to see if target is reached
tail
(create-table-aux (+ step current) step
(cons
(convert-to-temperature-pairs current scale)
tail)))))))
(create-table-aux min step '())))
(define (print-temperature-table min max step scale)
(letrec ((print-line-by-line (lambda (lines)
(let ((line (car lines)) (rest (cdr lines)))
(begin (display (car line))
(display "\t")
(display (cadr line))
(newline)
(if (not (eq? rest '()))
(print-line-by-line rest)
(displayln "-------")))))))
(print-line-by-line (reverse (create-table-in-range min max step scale)))))