HASKELL   25

rand

Guest on 22nd September 2022 12:26:52 AM

  1. -- CMPT 383, Fred Popowich, June 9, 2011
  2. import Random
  3. import List
  4. -- import some libraries for Random numbers and List operations (sort)
  5.  
  6. main :: IO ()
  7. main =  do
  8.     putStrLn "How many random integers do you want:"
  9.     count <- getLine
  10.     rand (read count) []
  11.  
  12. -- recursively call the rand function to produce a list of n random integers
  13. -- then call some other function to do something with this list
  14. -- NOTE: the function returns something of type IO (), so if you want to
  15. -- do something with the list of random integers, you do it by passing it
  16. -- in to another function
  17. rand :: Int -> [Int] -> IO ()
  18. rand n rlst = do
  19.     num <- randomRIO (1::Int, 52)
  20.     if n == 0
  21.         then doSomething rlst
  22.         else rand (n-1) (num:rlst)
  23.  
  24. -- if you wanted to then do something with this list of integers
  25. -- you could call or define a function. Here, the "something" was to print
  26. -- out a sorted list
  27. doSomething x =
  28.     putStrLn (show (sort x))
  29.  
  30. -- remember: use additional function calls to do something with the
  31. -- things you create

Raw Paste


Login or Register to edit or fork this paste. It's free.