I'm trying to write a simple Lehmer algorithm pseudorandom number generator to get a better understanding of how to handle IO values in Haskell and I keep getting type mismatches.
My code looks as follows:
import Data.Time.Clock.POSIX
generator :: Int -> Int -> Int -> Int
generator x i m
| i <= 0 = error "Index out of bounds"
| i == 1 = x * ( x `mod` m )
| otherwise = generator (generator x (i - 1) m) 1 m
pseudorand :: Int -> Int -> IO Int
pseudorand i m = do t <- round `fmap` getPOSIXTime
return ( \ i m -> generator t i m )
The generator function receives a seed, a number of iterations and a modulo parameter and runs a recursive modulo calculation where x_(i+1) = x_0 * x_i mod m, simple enough. Getting millisecond time as an IO Int is also doable. The trouble I'm running into is passing the IO Int onto my generator function afterwards.
The above code expects the an 'IO Int' but gets 'IO (Int -> Int -> Int)'. That is obviously the type of my generator function, so I tried
pseudorand :: Int -> Int -> IO Int
pseudorand i m = do t <- round `fmap` getPOSIXTime
return ( fmap generator t i m )
which expects an 'Int' but gets an 'Int -> Int'.
Why does it not expect inputs? Why does it not see the i and m?
Would also appreciate your input on how you'd write a code for this
Aucun commentaire:
Enregistrer un commentaire