Wednesday, 7 July 2010
F# vs Mathematica: red-black trees
Mathematica is an expensive commercial application sold as software for "interactive technical computing". In particular, the product centers around the Mathematica programming language which is a very powerful and dynamic language based upon term rewriting with excellent graphical capabilities and a wealth of useful functionality in its standard library. However, Microsoft's new F# programming language not only provides most of the useful functionality found in Mathematica but has many other benefits, not least that it is completely free!
We have previously compared F# and Mathematica in the context of 2D vector graphics. This post is the first in a series comparing these languages. Specifically, this post compares data structures. The F# programming language is designed for efficient compilation to native code whereas the Mathematica language is generally evaluated via term rewriting which is, essentially, a very inefficient form of interpretation. Consequently, F# code is typically 10-1,000× faster than Mathematica code unless computational burden can be shifted onto functions written in other languages (usually those in the vast standard library).
The latest Mathematica-related book, Mathematica Cookbook by Sal Mangano, contains many examples including a translation of Chris Okasaki's immutable red-black trees from Haskell to Mathematica augmented with a function to remove an element from a tree. Unfortunately, the new function written by Sal Mangano is incorrect and silently corrupts the shape of the tree. Thus, we shall focus on the subset of the code presented in Sal's book that is correct. This code may be written as follows in F#:
type color = Red | Black type 'a t = Node of color * 'a * 'a t * 'a t | Leaf let balance = function | Black, z, Node (Red, y, Node (Red, x, a, b), c), d | Black, z, Node (Red, x, a, Node (Red, y, b, c)), d | Black, x, a, Node (Red, z, Node (Red, y, b, c), d) | Black, x, a, Node (Red, y, b, Node (Red, z, c, d)) -> Node (Red, y, Node (Black, x, a, b), Node (Black, z, c, d)) | x -> Node x let insert x s = let rec ins = function | Leaf -> Node (Red, x, Leaf, Leaf) | Node (color, y, a, b) as s -> if x < y then balance (color, y, ins a, b) elif x > y then balance (color, y, a, ins b) else s match ins s with | Node (Red, y, a, b) -> Node (Black, y, a, b) | t -> t
The Mathematica code is significantly more complicated (714 vs 1514 bytes) for two main reasons:
- Mathematica has no types so it cannot express a type with an ordering, so a new rbTree data structure is created with an ordering and that must then be boxed and unboxed by hand.
- Mathematica prohibits top-level or-patterns so the same pattern is repeated four times in the balance function.
The simpler F# solution is also 20× faster at inserting 100k elements and 100× faster at computing the range of depths in the resulting tree.
In the next post, we'll start to compare the graphical capabilities of the two languages.
However, it may be a bit unfair to compare the two: F# is a young technology, born for today's computers and technologies (such as the .Net CLR). Mathematica is an old technology, and when it was new we were all in awe - or am I wrong?
It seems clear to me that there is no reason why I should use Mathematica when I can get use a (way more) versatile language that can be integrated with the other code that I write with no problems whatsoever. Calculation libraries similar to those used by Mathematica are easily available for .Net (and hence for F#), visual stuff like WPF is way more powerful,... Bit of a no-brainer, really.
And another thing: there's hundreds of books on Mathematica. There's very few on F# (four or five?), but they are all very good
@Anonymous: You expect me to do that when Sal did not extend me the same courtesy? If he wants to rebut my criticisms, he can do so on his own blog.
On another note, I have no idea who this Sal Mangano is. Apart from closely reminiscing the name of a member of the Sicilian mafia who used to drive the Italian prime minister's children to school in days gone by, I have never heard of the guy.
From what I understand, there seems to be an ongoing argument between the two of you. Sal, if you read this, please bear in mind that arguing with Jon is pretty useless: the probability of him being right is extremely high. This I learned through experience. Sal, take it from a fellow Italian: let it go :-)
I agree that Mathematica was awesome in its heyday but millions of people are still using it oblivious to the enormous advances easily available from free alternatives like F#. I still highly recommend Mathematica as an executive toy though.
Subscribe to Post Comments [Atom]
Links to this post:
Subscribe to Posts [Atom]