[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/XF5DR2Gc    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Jun 30:
; feet and inches

(define (feet-and-inches n)
  (if (zero? n) "0 feet 0 inches"
    (let* ((n (+ n 1/64))
           (feet (inexact->exact (floor (/ n 12))))
           (inches (inexact->exact (floor (- n (* feet 12)))))
           (32nds (/ (inexact->exact (floor (* (- n (* feet 12) inches) 32))) 32)))
      (string-append
        (if (zero? feet) "" (number->string feet))
        (if (zero? feet) "" (if (= feet 1) " foot" " feet"))
        (if (zero? inches) "" (if (positive? feet) " " ""))
        (if (zero? inches) "" (number->string inches))
        (if (zero? 32nds) ""
          (if (and (zero? feet) (zero? inches)) ""
            (if (zero? inches) " " " and ")))
        (if (zero? 32nds) "" (number->string 32nds))
        (if (and (zero? inches) (zero? 32nds)) ""
          (if (or (zero? inches) (and (= inches 1) (zero? 32nds))) " inch" " inches"))))))

(for-each
  (lambda (n)
    (display n) (display #\tab) (display (feet-and-inches n)) (newline))
  '(0 0.2785 1.6895 11.9999 12.2785 71.9999 72 72.3492 72.9999 73 73.0135 73.0185 73.8218))


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
0	0 feet 0 inches
0.2785	9/32 inch
1.6895	1 and 11/16 inches
11.9999	1 foot
12.2785	1 foot 9/32 inch
71.9999	6 feet
72	6 feet
72.3492	6 feet 11/32 inch
72.9999	6 feet 1 inch
73	6 feet 1 inch
73.0135	6 feet 1 inch
73.0185	6 feet 1 and 1/32 inches
73.8218	6 feet 1 and 13/16 inches


Create a new paste based on this one


Comments: