Advertisement
Guest User

Daily Programmer: Challenge #224 [Easy] Shuffling a List

a guest
Jul 21st, 2015
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     -- Comments start with two scores
  2.     -- Function application is done with spaces
  3.     -- What in many languages is "add(1,2)", in Haskell is "add 1 2"
  4.     -- Function application has priority over
  5.     -- boolean and mathematical operators
  6.  
  7.     module Main where
  8.  
  9.     import Data.List (permutations, (!!))
  10.     import System.Random (randomRIO)
  11.  
  12.     -- Recursive definition of factorial function
  13.    
  14.     -- Type declaration: the factorial function takes one Int argument
  15.     -- and returns and Int
  16.     factorial :: Int -> Int
  17.     -- Function definitions. Very close to mathematical notation.
  18.     factorial 0 = 1
  19.     factorial 1 = 1
  20.     factorial n = n * factorial (n - 1)
  21.  
  22.     -- For impure "variables" that require IO (i.e. inputList), <- is used
  23.     -- For pure "variables" that don't require IO, a let binding is used.
  24.  
  25.     -- The IO type represents side effects. It is a Monad, but that doesn't
  26.     -- matter in this example.
  27.     main :: IO ()
  28.  
  29.     -- Using "do" notation allows us to use a notation similar to that used
  30.     -- in imperative languages.
  31.     main = do
  32.       -- Read the single line of input as a list of strings using the getLine function
  33.       -- The "words" function returns a list containing the substrings that were
  34.       -- delimited by blank spaces (' ') on the input string
  35.       -- e.g. words "4 words and numbers" = ["4", "words", "and", "numbers"]
  36.       -- The fmap function allows us to apply the function "words" to the string
  37.       -- that the impure getLine function returns
  38.       inputList <- fmap words getLine
  39.      
  40.       -- The number of all possible permutations of a list is equal to
  41.       -- the factorial of the size the list
  42.       let sizeOfPermutations = factorial (length inputList)
  43.      
  44.       -- The Data.List library that we imported has a permutation function that,
  45.       -- given a list, returns a list containing all permutations of its argument
  46.       let perms = permutations inputList
  47.      
  48.       -- We need to pick a random permutation, so we produce a random number.
  49.       -- randomRIO stands for random "enumerable type" (i.e. ints, enums in other languages...)
  50.       -- and uses IO operations to seed itself
  51.       -- It generated a random element contained in the range specified in its argument
  52.       -- Similarly to many other programming languages, in the Haskell stardard libraries,
  53.       -- list are indexed starting at 0
  54.       randomPosition <- randomRIO (0, sizeOfPermutations - 1)
  55.      
  56.       -- The !! operator is used for indexing
  57.       -- In many other languages, this line would read shuffledList = perms[randomPosition]
  58.       let shuffledList = perms !! randomPosition
  59.      
  60.       -- Print can be used to output to the standard output
  61.       -- When outputing a string, the string is surrounded by quotation marks
  62.       -- When outputing a list, elements are separated by comma and the list is enclosed in square brackets
  63.       -- This format is not the one specified for the problem though...
  64.      
  65.       print shuffledList
  66.      
  67.       -- ... so let's fix the format:
  68.      
  69.       -- The unwords function does the oposite of what the previously discussed words function does
  70.       -- It takes a list of strings and return a string made up of the elements of the input
  71.       -- list separated by one blank space (' ')
  72.       let outputString = unwords shuffledList
  73.      
  74.       -- putStrLn (put string line) prints a string, without quotation marks,
  75.       -- to standard output and inserts a new line (\n)
  76.       putStrLn outputString
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement