I am learning two languages these days: Haskell and JavaScript. Haskell because it’s such an interesting language and JavaScript because I need to know it better than I do right now for work.
An exercise I like to do when I learn a new language is to write a cribbage point counting module. It’s not too hard to do, but it’s longer than the classical fib() and fact() functions.
In a somewhat old post (though only recently posted to reddit) Georg noticed that his CSS library was shorter in Haskell than in Python. Although my cribbage exercise is smaller, easier and more trivial than his CSS library, I noted the same thing.
The Haskell version is 133 lines, while the JavaScript version is 241 lines long. You can view the difference in the (big) image below.
I can’t point exactly why Haskell is so much shorter, but I think that pattern matching and currying have a lot to do with it. Also, I added some functions in JavaScript that are already available in Haskell and code to make usage a little less wordy (Haskell didn’t need anything like that.)
February 18, 2008 at 9:57 pm |
Shorter programs are fine, but how about the time it took you to write it ? Obviously, if you took 4 times as much time to write the Haskell one as the Javascript one, you’re not winning much. Performance might be an issue, too, but perhaps not in such small programs.
February 19, 2008 at 8:37 am |
I don’t think the Haskell program took more time. I didn’t measure, so it’s just a gut feeling.
March 5, 2008 at 3:21 am |
@SolarBear > Haskell is really quite easy to write programs in. I never felt that writing my program in Haskell was longer than it would be in any other language I practice.
As to the performance issue, Haskell is winning hand down on that point, I don’t even need to benchmark those two to tell you this, the fundamental difference is still too great, maybe in some years, Javascript interpreters will progress to perform only some times slower than compiled Haskell, but nowadays the difference is closer to at least twenty times or worse in real programs. Besides this Javascript program is written in the functional style to which Haskell is perfectly adapted and optimised for.
March 16, 2008 at 12:53 pm |
Any reason you repeat rank and suits array declaration in the javascript version?
August 10, 2008 at 11:15 am |
@Jedai: Try writing programs that require state, e.g. a random number generator.
@Laurent: To make it longer?
@gnuvince: I always approve of more cribbage in the world, but did it have to come as an enormous PNG? ;)
August 11, 2008 at 7:32 pm |
Here’s a shorter powerset function:
powerset = filterM (const [True, False])
What a gem!
November 22, 2008 at 8:31 am |
Interesting comparison.
A couple of things I’ve noticed in the JavaScript code (I didn’t read it carefully though).
Why aren’t you defining map/reduce functions/methods previous to defining sum/max/all? You could also use them in many parts of your JS implementation, reducing considerably the lines of code. Some browsers have those methods built in the array object also.
Also, a nicer way of defining prototype methods is by assigning an object (the prototype object) to that property, so instead of doing:
A.prototype.fun1 = ...;
A.prototype.fun2 = ...;
...
You could do:
A.prototype = {
fun1: function() { ...},
fun2: function() { ... }
};
November 22, 2008 at 8:41 am |
It’s all too easy to play golf with these things, but anyway..
IsFigure (x, _) = (fromEnum x) >= (fromEnum Jack)
and of course, card could be a data type with records, making the accessors unecessary
November 22, 2008 at 2:00 pm |
If you add Ord to the deriving list for Rank, you can then define
isFigure (x,_) = x >= jack
or even
isFigure = (>= jack) . fst