0

I am trying to make a generic function that returns a random value. I can make one working for floats, followed by another one working for ints... like this:

randomFloat :: StdGen -> Float-> Float-> (Float, StdGen)
randomFloat rndGen min max = randomR (min, max) rndGen :: (Float, StdGen)

randomInt :: StdGen -> Int-> Int-> (Int, StdGen)
randomInt rndGen min max = randomR (min, max) rndGen :: (Int, StdGen)

but I'd like to have one working for any relevant variable type. How can I do it? So far, I have this but this won't work:

randomNbr :: (Random a) => StdGen -> a -> a -> (a, StdGen)
randomNbr rndGen min max = randomR (min, max) rndGen :: (a, StdGen)
Mickael Bergeron Néron
  • 1,472
  • 1
  • 18
  • 31

1 Answers1

4

The a in your :: (a, StdGen) doesn't refer to the same a as in your function's type signature. Just remove :: (a, StdGen) and it will work.

More generally, if you want/need to use a type variable in an annotation in an expression like that, you need to enable GHC's ScopedTypeVariables extension.

Ørjan Johansen
  • 18,119
  • 3
  • 43
  • 53
  • @MickaelBergeronNéron When you define this function, Haskell infers that it has a polymorphic type that can be used for several `a`. But when you actually *use* it, one particular type has to be picked. Haskell will try to infer what type you need from how you use it, or if that fails try and choose a default, but without extensions `Random` cannot be defaulted. In recent GHCi versions `ExtendedDefaultRules` apply and you *don't* need to specify a return type e.g. for `randomR (1,3) (mkStdGen 42)`. – Ørjan Johansen Aug 21 '14 at 23:05
  • I see you deleted your question comment while I was typing, so I guess you figured it out; leaving my comment above in case it helps someone. – Ørjan Johansen Aug 21 '14 at 23:09