4 ms·
Ture, but notice how easy and natural it is in Haskell. You can do the same thing in Python, as noted in the comment by jordigh -- but it takes ~40 lines, as op
by crntaylor 13y ago
Ture, but notice how easy and natural it is in Haskell. You can do the same thing in Python, as noted in the comment by jordigh -- but it takes ~40 lines, as opposed to ~8 lines in Haskell. I'm willing to bet that it's even more verbose in C++, and I don't even want to think about doing it in Java.
- jordigh 13y agoI aimed for clarity and some extra features in mine (e.g. __repr__ and __str__) instead of code golfing it. I also used the actual Binet formula instead of just approximation. The only big Haskell feature here is a default implementation of binary exponentiation, but this is a library feature, not an innate feature of the language. It would be easy enough to augment existing standard Python libraries with such a corresponding feature.
- groovy2shoes 13y agoThe "deriving (Show)" gives you __repr__ and __str__ for free in Haskell.
- jordigh 13y agoIt gives you a default that isn't very pretty. Python also has a default that isn't very pretty.
- groovy2shoes 13y ago(1 % 2) :+ (1 % 2) Gives much more useful information than: <__main__.Qsqrt5 instance at 0xffee4d6c> In fact, it encodes the same information given by your __str__ with the added benefit that it can be read back into a Q5 value: 1/2 + 1/2 sqrt(5) Of course, yours would be nicer to display to someone unfamiliar with Haskell's type constructors. No arguing that. But it's also clear that Haskell's derived show trumps Python's default __str__/__repr__.
- crntaylor 13y agoIt wasn't meant to be a criticism of your implementation - just a comparison of the two languages for this particular task. Haskell happens to be very concise for writing mathematical code. I love Python too, but for different reasons (easy to prototype, great libraries, flexible). I've noticed that people with a mathematical background tend to be very attracted to Haskell. There's a great blog post by Dan Piponi enumerating eleven reasons to use Haskell as a mathematician: http://blog.sigfpe.com/2006/01/eleven-reasons-to-use-haskell-as.html http://blog.sigfpe.com/2006/01/eleven-reasons-to-use-haskell...
- jordigh 13y agoI have a mathematical background, and I am not that attracted to Haskell. And I've tried. It seems that Haskell attracts logicians, which are their own particular subspecies of mathematicians, but I think far more mainstream mathematicians are attracted to Python. Sage is a prominent example of how easy it is for mathematicians to get into Python.
- catnaroek 13y ago> It seems that Haskell attracts logicians, which are their own particular subspecies of mathematicians Or perhaps people who care about foundations in general.