[Haskell-cafe] Why I Love Haskell In One Simple Example
John Goerzen
jgoerzen at complete.org
Fri Jun 24 16:56:34 EDT 2005
I posted this on my blog at http://changelog.complete.org/node/339 but I
thought there may be some here that would find it of use.
I recently implemented some new Haskell numeric types that, instead of
performing calculations, can generate a rendering of the requested
calculation or store units with it.
Here you see a transcript of my session with a Haskell interpreter. The
mathematical statements I am entering after the ">" are standard Haskell
expressions, and, as I demonstrate, normally evaluate to a single
result.
Once I get a more powerful simplifier, I will probably write a LaTeX
exporting function as well.
The entire implementation of this, BTW, is less than 200 lines.
NumTest> 5 + 1 * 3
8
NumTest> prettyShow $ 5 + 1 * 3
"5+(1*3)"
NumTest> rpnShow $ 5 + 1 * 3
"5 1 3 * +"
NumTest> prettyShow $ 5 + 1 * 3
"5+(1*3)"
NumTest> prettyShow $ simplify $ 5 + 1 * 3
"5+3"
NumTest> prettyShow $ 5 * (Symbol "x") + 3
"(5*x)+3"
NumTest> 5 / 2
2.5
NumTest> (units 5 "m") / (units 2 "s")
2.5_m/s
NumTest> (units 5 "m") / 2
2.5_m
NumTest> 10 * (units 5 "m") / (units 2 "s")
25.0_m/s
NumTest> sin (pi/2)
1.0
NumTest> sin (units (pi/2) "rad")
1.0_1.0
NumTest> sin (units 90 "deg")
1.0_1.0
NumTest> (units 50 "m") * sin (units 90 "deg")
50.0_m
NumTest> ((units 50 "m") * sin (units 90 "deg")) :: Units (SymbolicManip Double)
50.0*sin(((2.0*pi)*90.0)/360.0)_m
NumTest> rpnShow $ dropUnits $ ((units 50 "m") * sin (units 90 "deg"))
"50.0 2.0 pi * 90.0 * 360.0 / sin *"
NumTest> (units (Symbol "x") "m") * sin (units 90 "deg")
x*sin(((2.0*pi)*90.0)/360.0)_m
Also, I defined this in my source file:
test :: forall a. (Num a) => a
test = 2 * 5 + 3
Now, it can be used:
NumTest> test
13
NumTest> rpnShow test
"2 5 * 3 +"
NumTest> prettyShow test
"(2*5)+3"
NumTest> test + 5
18
NumTest> prettyShow (test + 5)
"((2*5)+3)+5"
NumTest> rpnShow $ test + 5
"2 5 * 3 + 5 +"
You can grab the very early experimental code with
darcs get http://darcs.complete.org/num.
Haskell has no built-in support for numeric types with units, arbitrary
symbols carried through computations, etc. But it was trivial to add it.
This kind of extensibility is a key part of why Haskell is so amazing.
More information about the Haskell-Cafe
mailing list