-- Lecture 2 on Input/Output

import Random

-- Communication with files

-- The length of a file

lengthFile :: IO ()
lengthFile = 
  do { 
      fn <- getLine ;
      file <- readFile fn ;
      putStrLn ("File " ++ fn ++ " contains " ++ 
                show(length file) ++ " characters.\n")
     }

-- Reversing the content of a file and writing into another one

reverseFile :: FilePath -> FilePath -> IO ()
reverseFile fn1 fn2 = 
  do {
      str <- readFile fn1 ;
      writeFile fn2 (reverse str)
     }

-- Randomness

password :: IO ()
password = do {
               let {c = randomRIO ('a','z')} ;
               c1 <- c ; 
               c2 <- c ; 
               c3 <- c ; 
               c4 <- c ;
               putStrLn [c1,c2,c3,c4]
              }


password1 :: Int -> IO ()
password1 n = loop 0 ""  

  where

     loop :: Int -> String -> IO ()
     loop k pwd = if k >= n
                  then putStrLn pwd
                  else do {
                           c1 <- randomRIO ('a','z') ;
                           loop (k+1) (c1:pwd)
                          }

password2 :: IO ()
password2 = 
  do {
      n <- randomRIO (1,10) ;
      password1 n  
     }



