Category Archives: Programming

Why I Love Haskell In One Simple Example

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.

Today’s Reading + Perl Community

Cliff’s memories of Easter are a great read.

A study showing that a significant number of people exhibit violence towards their computer when it malfunctions, and another significant number of people attempt to sweet-talk it.

Shapr posted a link to the extremeperl mailing list. I found this post on the value of learning languages to be insightful.

Along a similar line, there’s an intriguing post on The Sequence from a Perl hacker looking at the Haskell community. I think he’s right.

These Perl people are really impressing me lately. My respect for the Perl commnuity has really shot upwards lately. And I am similarly disappointed in many of the directions Python is taking these days. Nice time to be using Haskell :-)

Haskell: The Humbling Language

Usually when I learn a new programming language, I’ll hang out on its mailing lists and IRC channels, learning from the answers given to other people’s questions, and asking my own. After a month or two, I usually feel fairly good with my abilities; that I could answer most of the questions, and understand most of the questions.

Well, I’ve been using Haskell for about 6 months now. I really like Haskell, and it’s a great language to use, and it’s already my preferred language.

But here’s what’s unique about Haskell. The more I use it, and the more I participate with the Haskell community, the more I realize just how much there is that I could learn. And it seems that I’m not alone with that feeling.

I wonder why Haskell is unique this way.