gusl: (Default)
[personal profile] gusl
(defun convert ()
  (format t "Fahrenheit: ")
  (let ((fahr (read)))
     (format t "Celsius: ~a~%" (/ (- fahr 32) 1.8))))


Thanks to Jabberwockey, randomly found at lisppaste.org

lisp infinite design regressions

Date: 2006-06-15 09:55 pm (UTC)
From: [identity profile] http://users.livejournal.com/_greg/
It is rare for me to see any lisp code without wanting to explore the design space of alternatives, which design space is combinatorially Vast (to use Dennett's term from "Darwin's Dangerous Idea").

I prefer LISPs closer to the Scheme family. I find Common Lisp to be a horror. My ideal LISPs are even simpler than Scheme, with a simpler extensible type system (no fancy numeric tower unless and as you dictate) and a bias towards purely functional, non-modifiable data.

If I were to explore unit conversions, I would want to use a packages for associating dimensions, units and precisions with values, and a constraint satisfaction package so that conversions would automatically be two-way. I might also want to add a GUI package so that one can explore conversions with sliders, graphs, etc. LISP is so flexible that all of these things are readily available, but in many different forms. You see how quickly my thoughts diverge!

If I were being as modest as I am able, and writing your converter in Scheme, I would still require some modularity, oriented towards separation of concerns and reusability. I would especially want to separate out the user-interface part, as this kind of tty-style interface is inherently poor. In short, I might write something like this:

(use-modules (srfi srfi-9))		; to load srfi-9 under Guile Scheme

,open srfi-9			;  to load srfi-9 under Scheme48

; see http://srfi.schemers.org/srfi-9/srfi-9.html
(define-record-type :unit-convert
  (make-unit-convert from to by)
  unit-convert?
  (from unit-convert-from)
  (to unit-convert-to)
  (by unit-convert-by)  )

(define (unit-convert-interact converter)
  (for-each display (list "Given in " (unit-convert-from converter) ": "))
  (let ( (value (read)) )
    (for-each display (list "Equivalent value in " (unit-convert-to converter) " is "
	  ( (unit-convert-by converter) value ) ) )
    (newline)
    ) )

(define fahrenheit-to-celsius
  (make-unit-convert 'fahrenheit 'celsius (lambda (x) (/ (- x 32) 1.8))) )

(unit-convert-interact fahrenheit-to-celsius)

February 2020

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags