mercredi 5 octobre 2016

What does getStdRandom $ randomR return?

I wrote a short Haskell program and compiled it without issues

import System.Random
func1=getStdRandom $ randomR ('A','Z')
main = do
  print =<< func1

But if I changed the tuple to (1,100), I have to add a type signature in order to successfully compile it.

import System.Random
func2 :: IO Int
func2=getStdRandom $ randomR (1,100)
main = do
  print =<< func2

The type of the functions are different.

Prelude System.Random> func1 = getStdRandom $ randomR ('A','Z')
Prelude System.Random> func2 = getStdRandom $ randomR (1,100)
Prelude System.Random> func3 = getStdRandom $ randomR (1,100) :: IO Int
Prelude System.Random> :t func1
func1 :: IO Char
Prelude System.Random> :t func2
func2 :: (Random a, Num a) => IO a
Prelude System.Random> :t func3
func3 :: IO Int
Prelude System.Random> 

Could someone tell me why two similar looking tuples (Char,Char) and (Int,Int) generate different type signature functions ?

And if I moved the function, it does not compile.

import System.Random
main = do
print =<< getStdRandom $ randomR ('A','Z')

I don't know why "print =<< func1" works but "print =<< getStdRandom $ randomR ('A','Z')" does not work if func1 and "getStdRandom $ randomR ('A','Z')" are the same thing?

Thank you eii




Aucun commentaire:

Enregistrer un commentaire