jeudi 25 juillet 2019

Haskell sorted random list is infinite

For a small project I want to use Haskell. Need batches of 4 random numbers between 0 and 9, and these [Int] needs to be sorted when done (using: Data.Sort). Code below keeps returning an infinite list despite take 4 xs.

import System.Random 
import Data.Sort 

randomList :: IO () 
randomList = do 
  generator <- getStdGen 
  let rndGen = randomRs (0,9) generator :: [Int]
  getFourDigits <- return (fourDigits rndGen) 
  putStrLn $ show getFourDigits

fourDigits :: Ord a => a -> [a]
fourDigits rndGen = sort $ take 4 (repeat rndGen)

Is this caused by dry-running in stack ghci?




Aucun commentaire:

Enregistrer un commentaire