5 ms·
This is mind-blowing. This comment is the best answer I've seen yet to the question, "Why learn Haskell?".
by leephillips 13y ago
This is mind-blowing. This comment is the best answer I've seen yet to the question, "Why learn Haskell?".
- colomon 13y agoThe only reason I haven't actually implemented a number type like this in Perl 6 (someone proposed it to me years ago) is I didn't really it had such a lovely and practical application.
- colomon 13y agoProps to Haskell, though -- p6 makes it easy to do, but Haskell is definitely more elegant here. Though maybe if I keep playing with it...
- jordigh 13y agoThis is kind of pedestrian for a mathematician, and there aren't any features in use here that are exclusive to Haskell. Basically, you're just definining a new field of numbers where you throw in the sqrt(5) into the rational numbers. You could very easily do this in Python too: http://ideone.com/9dHQV9 http://ideone.com/9dHQV9
- gaze 13y agogrr. this has nothing to do with haskell.
- crntaylor 13y agoTure, 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...