Gasoline Mileage Log
November 2, 2012
We assume that the input file has only two fields per line, mileage and gallons. Then we can use read
to read the two fields in order, converting them to numbers as it reads. Here’s the program:
(define (show-log file-name)
(display "Miles Gals Avg") (newline)
(display "------ ---- ----") (newline)
(with-input-from-file file-name
(lambda ()
(let* ((old (read)) (gals (read)))
(let loop ((old old))
(let* ((new (read)) (gals (read)))
(unless (eof-object? (peek-char))
(display new) (display " ")
(display gals) (display " ")
(display1 (/ (- new old) gals))
(newline)
(loop new))))))))
One of the weaknesses of Standard Scheme is its lack of display formatting capabilities:
(define (display1 x)
(let ((x (inexact->exact (floor (* 10 x)))))
(display (quotient x 10))
(display ".")
(display (modulo x 10))))
Here’s the output:
> (show-log log)
Miles Gals Avg
------ ---- ----
107934 10.4 24.5
108197 12.2 21.5
108518 13.8 23.2
108762 12.2 20.0
109028 11.5 23.1
109306 12.4 22.4
109589 11.1 25.4
You can run the program at http://programmingpraxis.codepad.org/Ig82n9QQ.
[…] today’s Programming Praxis exercise, our goal is to calculate and show the gas mileage given an input file […]
My Haskell solution (see http://bonsaicode.wordpress.com/2012/11/02/programming-praxis-gasoline-mileage-log/ for a version with comments):
My Python solution.
[…] Pages: 1 2 […]