Guest User

http://programmingpraxis.com/2012/09/07/the-first-two-progra

a guest
Sep 7th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.88 KB | None | 0 0
  1. #lang racket
  2.  
  3. (define (fahrenheit->celsius f)
  4.   (/ (- f 32) 1.8))
  5.  
  6. (define (celsius->fahrenheit c)
  7.   (+ 32 (* c 1.8)))
  8.  
  9. ;; return a list (fahrenheit celsius)
  10. (define (convert-to-temperature-pairs n scale)
  11.   (cond ((eq? scale 'fahrenheit) (list n (inexact->exact (round (fahrenheit->celsius n)))))
  12.         ((eq? scale 'celsius) (list (inexact->exact (round (celsius->fahrenheit n))) n))
  13.         (else '())))
  14.  
  15. (define (create-table-in-range min max step scale)
  16.   (letrec ((create-table-aux (lambda (current step tail)
  17.                                (if (>= 0 step) ;; guard for negative or zero increments
  18.                                  '()
  19.                                  (if (> current max) ;; check to see if target is reached
  20.                                      tail
  21.                                      (create-table-aux (+ step current) step
  22.                                                        (cons
  23.                                                         (convert-to-temperature-pairs current scale)
  24.                                                         tail)))))))
  25.     (create-table-aux min step '())))
  26.    
  27. (define (print-temperature-table min max step scale)
  28.   (letrec ((print-line-by-line (lambda (lines)
  29.                                  (let ((line (car lines)) (rest (cdr lines)))
  30.                                    (begin (display (car line))
  31.                                           (display "\t")
  32.                                           (display (cadr line))
  33.                                           (newline)
  34.                                           (if (not (eq? rest '()))
  35.                                               (print-line-by-line rest)
  36.                                               (displayln "-------")))))))
  37.     (print-line-by-line (reverse (create-table-in-range min max step scale)))))
Add Comment
Please, Sign In to add comment