[Haskell-beginners] Unit conversion

Tom Murphy amindfv at gmail.com
Thu Mar 17 22:17:01 CET 2011


Hi All,
     Is there a good way to easily convert between units?

     For example, let's say I have a data type:
     data Volumes = Teaspoon | Tablespoon | Slice | FluidOunces

     ... and I want to define an infix function '<+>', which adds together
amounts of food:
     (1, Slice, cake) <+> (1, Volume Tablespoon, sugar) <+> (32, Volume
FluidOunces, almondMilk)

     Which would return:
     (3200, Teaspoons)

     What is the most efficient way to define equivalency/conversion between
these measures?

     I remember an interesting method for celsius-farenheit conversion in
Higher-Order Perl, using function composition, but that was between only 2
units. I'd like something where I don't have to provide n^2 definitions.

Thank you!
Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110317/28c75739/attachment.htm>


More information about the Beginners mailing list