module L3B where import System.Random(randomRIO) -- Lecture 3B -- IO -- David Sands -- What is a function? diceRoll = randomRIO diceRange where diceRange = (1,6) :: (Int,Int) -- **** -- Small examples of IO -- writeFile, putStr -- * Building instructions vs running instructions -- instructions with results: -- Readfile -- putStr putStrLn -- building complex instructions from simple ones -- joining instructions, simple form: verboseWritefile :: FilePath -> String -> IO() verboseWritefile file contents = do putStrLn ("Writing file: " ++ file) putStrLn ("Contents: " ++ take 10 contents) writeFile file contents putStrLn "Done!" -- Joining instructions, using results -- copyFile copyFile :: FilePath -> FilePath -> IO() copyFile fromFile toFile = do s <- readFile fromFile writeFile toFile s -- The function return lengthFile :: FilePath -> IO Int lengthFile file = do s <- readFile file return (length s) -- Select a random word from a file -- (assuming the file is not empty) -- randomWord :: ?? -- IO t is a first-class citizen! -- doTwice -- doNot -- find longest Word in the file "/usr/share/dict/words" dict :: FilePath dict = "/usr/share/dict/words"