3 ms·
There are a couple of packages in Haskell that let you do this, including unittyped[1]. It lets you write code like: *Main> 2 meter + (1 meter / second) *
by tikhonj 14y ago
There are a couple of packages in Haskell that let you do this, including unittyped[1]. It lets you write code like:
*Main> 2 meter + (1 meter / second) * 5 second
7.0 m
A really cute feature is that prefixes like centi and deci are functions:
*Main> gallon `as` (cubic (deci meter))
4.546089999999999 dm⋅dm⋅dm⋅#
It gives you an error if the dimensions don't match, but unfortunately the error messages are hideous.
Anybody can define new units just by giving a conversion factor and a string to display. These new units immediately work with other units. You can probably define new "dimensions" as well, but that's bound to be more involved.
All the units are represented as empty types, so it should have no overhead at runtime.
So it is very nice syntactically, with the only real problem being the error messages and the fact that it uses a whole bunch of extensions and only supports very new versions of GHC. I think it's pretty cool.
- cynwoody 14y agoAccording to Google, 1 gallon is 3.78541178 cubic decimeters[1]. [1] https://www.google.com/search?q=1+gallon+in+cubic+decimeters https://www.google.com/search?q=1+gallon+in+cubic+decimeters
- tikhonj 14y agoIt's an imperial gallon[1]. As to why it's an imperial gallon, or what an imperial gallon really is, or who uses it--don't ask me. I'm a very metric sort of person :P. [1]: https://www.google.com/search?q=imperial+gallon+in+cubic+decimeter https://www.google.com/search?q=imperial+gallon+in+cubic+dec...