Numeric programming on toy problems in Haskell
Dear Tomi, all, I agree with everyone on this list that there is a good reason for being picky about numeric types. However, when you're simply trying to understand a small problem or puzzle, the rather intricate numeric part of the type system can be prohibitively complex. I'm no Haskell guru, but I don't consider myself a novice anymore. Still, I tend to waste time on numeric stuff when trying to understand a simple and small problem. To avoid this, I wrote a Number module that provides a type Number that is of all of the standard numeric type classes. Especially for toy problems - I keep repeating this, because my Number module is NOT FIT for production code - it is the lazy man's solution to all the fuss about numbers. I'm lazy, me like. You can have a look at the Number module here: http://www.cs.utwente.nl/~holzensp/Number.hs Now, whenever I've hacked together a small program, it nearly always works. When it doesn't, it generally complains about some numeric value somewhere. I just stick a "toNumber" in front, et voilà! :D Regards, Philip PS. I know, people will want to kill me for putting all numbers back together in a sluggish single type, but in my defense: aren't we all a tiny bit lazy every once in a while?
Philip K.F. Hölzenspies wrote:
You can have a look at the Number module here:
http://www.cs.utwente.nl/~holzensp/Number.hs
Now, whenever I've hacked together a small program, it nearly always works. When it doesn't, it generally complains about some numeric value somewhere. I just stick a "toNumber" in front, et voilà! :D
It uses Rational as representation, temporarily switching to Double when it needs to carry out inexact/irrational computations such as (pi) and (sin). Integral looks reasonable (are those really supposed to be div and mod in defining quotRem, not quot and rem? I'm not sure). There's no(?) excuse for using GHC extensions... just use (Prelude.)fromRational instead of GHC.Float.fromRat (if using GHC, it will optimize to the same thing, at least if optimizations are on); and remove {-# OPTIONS -fglasgow-exts #-}. I like the design. (Have you seen Scheme's (dynamically typed) numeric ladder? It adds a separate, additional concept of being able to tell, from a numeric value, whether it is exact or not - e.g. for this Number implementation, (1 + 1/3) is exact, and (sin pi) isn't, I think.) Isaac
participants (2)
-
Isaac Dupree -
Philip K.F. Hölzenspies