r/programming • u/ttsiodras • Dec 23 '12
Simulating a solar system with Python
http://users.softlab.ntua.gr/~ttsiod/gravityRK4.html
244
Upvotes
r/programming • u/ttsiodras • Dec 23 '12
View all comments
4
u/Peaker Dec 24 '12 edited Dec 24 '12
I have a habit of transliterating Python code to Haskell.
The Haskell code.
And due to a bit of silliness in Haskell's GL library, here's an extra tiny wrapper it depends on, Vector2.hs.
I think the Haskell code turns out cleaner than the Python code.
But most importantly - it gets static type checking without additional costs!
To reiterate, the Python/Haskell versions are roughly the same size, and despite this, the Haskell code is fully statically typed.
One nice thing is the Haskell one takes 10% CPU here and the Python one takes 50% (without psyco). Haven't profiled to figure out why.
EDIT: Here are some comparisons of the length of both:
If you remove the silly Vector2 wrapper that exists in libraries anyway (wanted light-weight dependencies):
Haskell wc: 231 1161 8612
If you ignore all the imports which don't have the ordinary cost of other code, you get:
I find it pretty amazing that you get more safety and less code at the same time. This in a simulation, which is often cited as appropriate for OO programming, and not FP.