type classes and generality

Fergus Henderson fjh@cs.mu.oz.au
Tue, 10 Jul 2001 11:21:46 +1000


On 09-Jul-2001, Alexander V. Voinov <avv@quasar.ipa.nw.ru> wrote:
> Hi All,
> 
> Fergus Henderson wrote:
> > Ah, now I think I understand your problem.  You want to `random' to
> > generate random numbers that span all the possible values of the type
> > within the range [0, 1], or at least a substantial subset, but the "Real"
> > class doesn't let you generate any numbers other than integers.
> > The above approach will give you random numbers from `random', but they
> > will only ever be 0 or 1, so maybe they are not as random as you needed! ;-)
> 
> Each pseudorandom generator generates a countable sequence of values,
> which is isomorphic to a sequence of integers. In good old (Turbo)C we
> got something between 0 and MAXINT and then divided by (double)MAXINT.
> Can't _this_ be done in Haskell?

Sure, it can be done, but not on a value whose type is constrained only by
the "Real" class, because the "Real" class doesn't have any division operator!

Haskell's "/" operator is a member of the "Floating" class, and "Floating"
is not a base class of "Real".  It is a base class of the "RealFrac"
class, so you could use that approach for "RealFrac"... but the original
poster was asking for the solution to a more difficult problem.

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.